【问题标题】:C#: System.Runtime.IntropServices.COMException: Retrieving the COM class factory for component with CLSIDC#:System.Runtime.IntropServices.COMException:检索具有 CLSID 的组件的 COM 类工厂
【发布时间】:2013-01-29 09:31:20
【问题描述】:

我正在开发一个 MS word 模板生成器,它的工作原理是这样的:

用户打开网页填写一些文本框,标记一些复选框,然后单击按钮,打开word文档(我之前定义的模板,其中包含所需的所有书签),使用他在网页中输入的内容,就是这样在文档中,并且字段和复选框被标记为他标记的,他只需要打印文档。它在我的电脑上完美运行

但是我在服务器上测试的时候,出现了错误:

我的代码: 我已包含参考 (using Word = Microsoft.Office.Interop.Word;)

private void CreateWordDocument(int DepositNo)
        {
            Random RandomClass = new Random();
            int randomInt = RandomClass.Next();
            string templateName = "Form15Template.docx";
            string docFileName = "Form15_" + DepositNo.ToString() + "_" + randomInt.ToString() + ".docx";
            string destenation = "D:\\_Documents\\Form15\\";
            File.Copy(Server.MapPath(@"_Documents/" + templateName), destenation + docFileName, true); // 


            String POBox = "9769";
            String City = "الرياض"; 
            String PostalCode = "11423";
            String Address = "ص.ب " + POBox + " " + City + " " + PostalCode;

            // Modify Checkboxes variables in Doc to true if they have been checked

            if (RadioButtonListResult.SelectedValue.Equals("اجتاز"))
                A = true;
            if (RadioButtonListResult.SelectedValue.Equals("تعديل") || RadioButtonListResult.SelectedValue.Equals("لم يجتز"))
                B = true;
            if (CheckBoxAllElements.Checked)
                AllElements = true;
            if (CheckBoxElements.Checked)
                Elements = true;
            if (CheckBoxEdit.Checked)
                Edit = true;
            if (CheckBoxBoxPage.Checked)
                BoxPage = true;
            if (CheckBoxRequestArabic.Checked)
                RequestArabic = true;
            if (CheckBoxMultiInvention.Checked)
                MultiInvention = true;
            if (CheckBoxRecency.Checked)
                Recency = true;
            if (CheckBoxInventiveStep.Checked)
                InventiveStep = true;
            if (CheckBoxImplemented.Checked)
                Implemented = true;
            if (CheckBoxC.Checked)
                C = true;
            if (CheckBoxD.Checked)
                D = true;

            object bA = "A", bB = "B", bC = "C", bD = "D", bAllElements = "AllElements", bElements = "Elements",
                bRecency = "Recency", bInventiveStep = "InventiveStep", bImplemented = "Implemented",
                bApprove = "Approve", bEdit = "Edit", bBoxPage = "BoxPage", bRequestArabic = "RequestArabic",
                bMultiInvention = "MultiInvention", bObjection = "Objection", bMark = "Mark",
                bRejection = "Rejection";


            //Start Word and open template.
            Word._Application oWord;
            Word._Document oDoc;
            oWord = new Word.Application();
            oWord.Visible = true;
            object FileName = @"C:\Users\aalhammad\Documents\Visual Studio 2010\Projects\FormEditing\FormEditing\_Documents\Form15Template.docx";
            object oMissing = System.Reflection.Missing.Value;
            oDoc = oWord.Documents.Open(ref FileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

            // ============================================== CheckBoxes ====================================================
            // Modifying CheckBoxes in Document

            oDoc.FormFields.get_Item(ref bA).CheckBox.Value = A;
            oDoc.FormFields.get_Item(ref bB).CheckBox.Value = B;
            oDoc.FormFields.get_Item(ref bC).CheckBox.Value = C;
            oDoc.FormFields.get_Item(ref bD).CheckBox.Value = D;
            oDoc.FormFields.get_Item(ref bAllElements).CheckBox.Value = AllElements;
            oDoc.FormFields.get_Item(ref bElements).CheckBox.Value = Elements;
            oDoc.FormFields.get_Item(ref bRecency).CheckBox.Value = Recency;
            oDoc.FormFields.get_Item(ref bInventiveStep).CheckBox.Value = InventiveStep;
            oDoc.FormFields.get_Item(ref bImplemented).CheckBox.Value = Implemented;
            oDoc.FormFields.get_Item(ref bApprove).CheckBox.Value = Approve;
            oDoc.FormFields.get_Item(ref bEdit).CheckBox.Value = Edit;
            oDoc.FormFields.get_Item(ref bBoxPage).CheckBox.Value = BoxPage;
            oDoc.FormFields.get_Item(ref bRequestArabic).CheckBox.Value = RequestArabic;
            oDoc.FormFields.get_Item(ref bMultiInvention).CheckBox.Value = MultiInvention;
            oDoc.FormFields.get_Item(ref bObjection).CheckBox.Value = Objection;
            oDoc.FormFields.get_Item(ref bMark).CheckBox.Value = Mark;
            oDoc.FormFields.get_Item(ref bRejection).CheckBox.Value = Rejection;


            // ============================================== BookMarks =====================================================

            String ApplicantName = "شركة الزيت العربية السعودية",
                AgentName = "مكتب سليمان العمار للمحاماة والاستشارات القانونية",
                AppDate = "13/01/2013",
                Title = "حماية خط تدفق رأس بئر‏ ونظام اختبار باستخدام وحدة تحكم في سرعة مضخة كهربائية مغمورة وصمام فاصل يستخدم عند الطوارئ";

            // find bookmark

            object oBookmark = "DepositNO"; // name of bookmark in doc
            oDoc.Bookmarks.get_Item(ref oBookmark).Range.Text = GetDepositNO(DepositNo);
            oBookmark = "AppDate";
            oDoc.Bookmarks.get_Item(ref oBookmark).Range.Text = AppDate;
            oBookmark = "Title";
            oDoc.Bookmarks.get_Item(ref oBookmark).Range.Text = Title;
            oBookmark = "ApplicantName";
            oDoc.Bookmarks.get_Item(ref oBookmark).Range.Text = ApplicantName; 
            oBookmark = "AgentName";
            oDoc.Bookmarks.get_Item(ref oBookmark).Range.Text = AgentName;
            oBookmark = "Address";
            oDoc.Bookmarks.get_Item(ref oBookmark).Range.Text = Address;
            oBookmark = "AgentName01";
            oDoc.Bookmarks.get_Item(ref oBookmark).Range.Text = AgentName;
            oBookmark = "Times";
            oDoc.Bookmarks.get_Item(ref oBookmark).Range.Text = Times;
            oBookmark = "NumOfElements";
            oDoc.Bookmarks.get_Item(ref oBookmark).Range.Text = NumOfElements;
            oBookmark = "txtElements";
            oDoc.Bookmarks.get_Item(ref oBookmark).Range.Text = txtElements;
            oBookmark = "txtRecency";
            oDoc.Bookmarks.get_Item(ref oBookmark).Range.Text = txtRecency;
            oBookmark = "txtInventiveStep";
            oDoc.Bookmarks.get_Item(ref oBookmark).Range.Text = txtInventiveStep;
            oBookmark = "txtImplemented";
            oDoc.Bookmarks.get_Item(ref oBookmark).Range.Text = txtImplemented;
            oBookmark = "ElementsC";
            oDoc.Bookmarks.get_Item(ref oBookmark).Range.Text = ElementsC;
            oBookmark = "ElementsD1";
            oDoc.Bookmarks.get_Item(ref oBookmark).Range.Text = ElementsD1;
            oBookmark = "ElementsD2";
            oDoc.Bookmarks.get_Item(ref oBookmark).Range.Text = ElementsD2;
            oBookmark = "txtBoxPage";
            oDoc.Bookmarks.get_Item(ref oBookmark).Range.Text = txtBoxPage;
            oBookmark = "Fees";
            oDoc.Bookmarks.get_Item(ref oBookmark).Range.Text = Fees;
            oBookmark = "DepositNO01";
            oDoc.Bookmarks.get_Item(ref oBookmark).Range.Text = GetDepositNO01(DepositNo);
            oBookmark = "ExamTime";
            oDoc.Bookmarks.get_Item(ref oBookmark).Range.Text = ExamTime;
            oBookmark = "ExamTime01";
            oDoc.Bookmarks.get_Item(ref oBookmark).Range.Text = ExamTime;
object saveFileName = "D:\\_Documents\\Form15\\" + "Form15 " + DepositNo.ToString() + "_" + randomInt.ToString() + ".docx";

            oDoc.SaveAs(ref saveFileName, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing);

知道我正在研究:Visual Studios C# 2010 和 Microsoft .NET Framework 4.0 在服务器上是:Visual Studio C# 2005, MS Framework 2.0

知道可能是什么问题吗?

【问题讨论】:

  • Web 服务器上是否安装了 MS Office..? you cannot redistribute the Microsoft Office libraries when deploying your application. This is stated in the clause that allows you to develop applications using Microsoft Office (I actually just discovered this a few minutes ago). This means that whenever you develop using Microsoft Office components your application will only work if Office has been installed on the system that you are deploying your application to. In your case, it means that you have to install Office onto the web server.

标签: c# visual-studio com ms-word clsid


【解决方案1】:

您的问题的临时解决方案是安装 Microsoft Word(确保在安装过程中选择 .NET Programmability Support 组件)并以运行 Web 应用程序的用户身份运行该应用程序至少一次。此用户必须是具有桌面的交互式用户。

请注意,如果您想自动化 Office,则需要安装它 - 您可能部署的互操作程序集只是一个包装器,不包含实际代码。

但是,请记住,Microsoft Office 应用程序的非交互式自动化很棘手,并且不受 Microsoft 官方支持。它需要比您在 MSDN 中的自动化示例中找到的更多代码才能使事情变得可靠和稳定。诸如创建交互式桌面、维护单词实例或考虑阻止弹出消息之类的事情都需要在您的代码中明确实现。

更好的选择是使用 Open XML SDK 创建您的文档(尽管应该知道在当前 SDK 版本中生成不是线程安全的)。

【讨论】:

    【解决方案2】:

    这是您在网络服务器中使用的用户帐户的问题,您必须更改它:

    //
    // MessageId: CO_E_RUNAS_LOGON_FAILURE
    //
    // MessageText:
    //
    // The server process could not be started because the configured identity 
    // is incorrect. Check the username and password.
    //
    #define CO_E_RUNAS_LOGON_FAILURE         _HRESULT_TYPEDEF_(0x8000401AL)
    

    请注意,Microsoft 强烈不鼓励在 Web 服务器上使用 Office 程序。您将为访问您的网页的每个用户创建一个 Word 实例。 Word 是一个繁重的过程,仅用于个人工作站。即使在那里,它也像一个单例进程一样阻止用户启动多个 Word 实例。这在像您这样的互操作场景中不起作用。只需少量访问者,您的服务器就会因大量 Word 副本强加的庞大系统需求而崩溃。

    【讨论】:

      【解决方案3】:

      Check dcomcnfg 查看是否设置为“交互式用户”。

      【讨论】:

        猜你喜欢
        • 2011-05-14
        • 2012-02-17
        • 2023-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-25
        • 1970-01-01
        相关资源
        最近更新 更多