使用 ASP.NET C# 的 Microsoft Word 报告模板

发布于 2024-12-02 14:58:53 字数 12006 浏览 0 评论 0原文

当我在本地计算机上以调试模式运行时,一切正常,但在服务器上我收到了这样的错误。

检索具有 CLSID 的组件的 COM 类工厂 由于以下原因,{00020906-0000-0000-C000-000000000046} 失败 错误:80080005。

我的服务器是Windows 2008 64位,Office 2007,我的代码是这样的

private void GenerateWords(string sPO, string sSup)
    {
        Object oMissing = System.Reflection.Missing.Value;
        Object oTrue = true;
        Object oFalse = false;
        Object savechanges = true;

        Word.ApplicationClass oWord = new Word.ApplicationClass();
        Word.Document oWordDoc = new Word.Document();
        oWord.Visible = true;
        Object oTemplatePath = Server.MapPath("Reports/Word/PurchaseOrder.docx");            

        oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);            
        oWordDoc.Activate();

        foreach (Word.Field myMergeField in oWordDoc.Fields)
        {
            iTotalFields++;
            Word.Range rngFieldCode = myMergeField.Code;
            String fieldText = rngFieldCode.Text;

            // Start filling information in Word file
            if (fieldText.StartsWith(" MERGEFIELD"))
            {
                Int32 endMerge = fieldText.IndexOf("\\");
                Int32 fieldNameLength = fieldText.Length - endMerge;
                String fieldName = fieldText.Substring(11, endMerge - 11);

                fieldName = fieldName.Trim();

                if (fieldName == "PONo")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(sPO);
                }

                if (fieldName == "SupNo")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(sSup);
                }

                if (fieldName == "VendorID")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["VendorID"].ToString().Trim());
                }

                if (fieldName == "VName")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Name"].ToString().Trim());
                }

                if (fieldName == "Contact")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Contact"].ToString().Trim());
                }

                if (fieldName == "Designation")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Designation"].ToString().Trim());
                }

                if (fieldName == "Tel")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Tel"].ToString().Trim());
                }

                if (fieldName == "Fax")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Fax"].ToString().Trim());
                }

                if (fieldName == "PODate")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["PODate"].ToString().Trim());
                }

                if (fieldName == "ClientName")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["ClientName"].ToString().Trim());
                }

                if (fieldName == "JobDescription")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["JobDescription"].ToString().Trim());
                }

                if (fieldName == "JobNo")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["JobNo"].ToString().Trim());
                }

                if (fieldName == "CostCode")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["CostCode"].ToString().Trim());
                }

                if (fieldName == "SchDlvy")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["SchDlvy"].ToString().Trim());
                }

                if (fieldName == "DlvyPoint")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["DlvyPoint"].ToString().Trim());
                }

                if (fieldName == "Amount")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Amount"].ToString().Trim());
                }

                if (fieldName == "tbl")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeParagraph();
                    Word.Table tbl = oWordDoc.Tables.Add(rngFieldCode, 1, 5, ref oMissing, ref oMissing);
                    //oWordDoc.Tables.Add(rngFieldCode, dtItems(sPO, sSup).Rows.Count, 5, ref oMissing, ref oMissing);

                    //SET HEADER
                    SetHeadings(tbl.Cell(1, 1), "Item No.");
                    SetHeadings(tbl.Cell(1, 2), "Description");
                    SetHeadings(tbl.Cell(1, 3), "Unit");
                    SetHeadings(tbl.Cell(1, 4), "Unit Price");
                    SetHeadings(tbl.Cell(1, 5), "Amount");
                    //END SET HEADER

                    //Add Row
                    for (int i = 0; i < dtItems(sPO, sSup).Rows.Count; i++)
                    {
                        Word.Row newRow = tbl.Rows.Add(ref oMissing);
                        newRow.Range.Font.Bold = 0;
                        newRow.Range.Underline = 0;
                        newRow.Range.ParagraphFormat.Alignment =
                        Word.WdParagraphAlignment.wdAlignParagraphCenter;

                        newRow.Cells[1].Range.Text = dtItems(sPO, sSup).Rows[i][3].ToString();
                        newRow.Cells[2].Range.Text = dtItems(sPO, sSup).Rows[i][4].ToString();
                        newRow.Cells[3].Range.Text = dtItems(sPO, sSup).Rows[i][8].ToString();
                        newRow.Cells[4].Range.Text = dtItems(sPO, sSup).Rows[i][10].ToString();
                        newRow.Cells[5].Range.Text = dtItems(sPO, sSup).Rows[i][11].ToString();
                    }
                    //END ROW

                    oWord.Selection.TypeParagraph();
                }

                if (fieldName == "TItems")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtTotal(sPO, sSup).Rows[0]["Unit"].ToString().Trim());
                }

                if (fieldName == "Discount")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtTotal(sPO, sSup).Rows[0]["Discount"].ToString().Trim());
                }

                if (fieldName == "TAmount")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtTotal(sPO, sSup).Rows[0]["Amount"].ToString().Trim());
                }

                if (fieldName == "Summary")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Amount"].ToString().Trim());
                }

                if (fieldName == "ReqNo")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["ReqNo"].ToString().Trim());
                }

                if (fieldName == "RevNo")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["RevNo"].ToString().Trim());
                }
            }
        }
        // End filling information in Word file

        Object oSaveAsFile = (Object)Server.MapPath("Reports/Word/tmp2.docx");
        oWordDoc.SaveAs(ref oSaveAsFile, 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);

        oWordDoc.Close(ref savechanges, ref oMissing, ref oMissing);
        oWord.Application.Quit(ref savechanges, ref oMissing, ref oMissing);

        //foreach (Process p in System.Diagnostics.Process.GetProcessesByName("winword"))
        //{
        //    try
        //    {
        //        if (p.ProcessName == "WINWORD")
        //        {
        //            if (!p.HasExited)
        //            {
        //                p.Kill();
        //                p.WaitForExit(); // possibly with a timeout
        //            }
        //        }
        //        else
        //        {
        //            lblMessage.Text = "cannot kill. try again!";
        //        }
        //    }
        //    catch (Win32Exception winException)
        //    {
        //        //process was terminating or can't be terminated - deal with it    
        //        Session["error"] = winException.Message;
        //        Response.Redirect("MessageBoard.aspx");
        //    }
        //    catch (InvalidOperationException invalidException)
        //    {
        //        //process has already exited - might be able to let this one go  
        //        Session["error"] = invalidException.Message;
        //        Response.Redirect("MessageBoard.aspx");
        //    }
        //}   

        Response.ClearContent();
        Response.ClearHeaders();
        Response.ContentType = "application/msword";
        Response.WriteFile(Server.MapPath("Reports/Word/tmp2.docx"), false);
        Response.Flush();
        Response.Close();
    }

并且我遵循给予 Blog.Crowe.co.nz 但仍然有问题,从上个月开始我就无法解决这个问题。如果可以的话,请帮助我。谢谢

When I run with debugging mode in my local machine, it's okay but on server I got an error like this.

Retrieving the COM class factory for component with CLSID
{00020906-0000-0000-C000-000000000046} failed due to the following
error: 80080005.

My server is Windows 2008 64 bit, Office 2007 and my code is like this

private void GenerateWords(string sPO, string sSup)
    {
        Object oMissing = System.Reflection.Missing.Value;
        Object oTrue = true;
        Object oFalse = false;
        Object savechanges = true;

        Word.ApplicationClass oWord = new Word.ApplicationClass();
        Word.Document oWordDoc = new Word.Document();
        oWord.Visible = true;
        Object oTemplatePath = Server.MapPath("Reports/Word/PurchaseOrder.docx");            

        oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);            
        oWordDoc.Activate();

        foreach (Word.Field myMergeField in oWordDoc.Fields)
        {
            iTotalFields++;
            Word.Range rngFieldCode = myMergeField.Code;
            String fieldText = rngFieldCode.Text;

            // Start filling information in Word file
            if (fieldText.StartsWith(" MERGEFIELD"))
            {
                Int32 endMerge = fieldText.IndexOf("\\");
                Int32 fieldNameLength = fieldText.Length - endMerge;
                String fieldName = fieldText.Substring(11, endMerge - 11);

                fieldName = fieldName.Trim();

                if (fieldName == "PONo")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(sPO);
                }

                if (fieldName == "SupNo")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(sSup);
                }

                if (fieldName == "VendorID")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["VendorID"].ToString().Trim());
                }

                if (fieldName == "VName")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Name"].ToString().Trim());
                }

                if (fieldName == "Contact")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Contact"].ToString().Trim());
                }

                if (fieldName == "Designation")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Designation"].ToString().Trim());
                }

                if (fieldName == "Tel")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Tel"].ToString().Trim());
                }

                if (fieldName == "Fax")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Fax"].ToString().Trim());
                }

                if (fieldName == "PODate")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["PODate"].ToString().Trim());
                }

                if (fieldName == "ClientName")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["ClientName"].ToString().Trim());
                }

                if (fieldName == "JobDescription")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["JobDescription"].ToString().Trim());
                }

                if (fieldName == "JobNo")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["JobNo"].ToString().Trim());
                }

                if (fieldName == "CostCode")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["CostCode"].ToString().Trim());
                }

                if (fieldName == "SchDlvy")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["SchDlvy"].ToString().Trim());
                }

                if (fieldName == "DlvyPoint")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["DlvyPoint"].ToString().Trim());
                }

                if (fieldName == "Amount")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Amount"].ToString().Trim());
                }

                if (fieldName == "tbl")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeParagraph();
                    Word.Table tbl = oWordDoc.Tables.Add(rngFieldCode, 1, 5, ref oMissing, ref oMissing);
                    //oWordDoc.Tables.Add(rngFieldCode, dtItems(sPO, sSup).Rows.Count, 5, ref oMissing, ref oMissing);

                    //SET HEADER
                    SetHeadings(tbl.Cell(1, 1), "Item No.");
                    SetHeadings(tbl.Cell(1, 2), "Description");
                    SetHeadings(tbl.Cell(1, 3), "Unit");
                    SetHeadings(tbl.Cell(1, 4), "Unit Price");
                    SetHeadings(tbl.Cell(1, 5), "Amount");
                    //END SET HEADER

                    //Add Row
                    for (int i = 0; i < dtItems(sPO, sSup).Rows.Count; i++)
                    {
                        Word.Row newRow = tbl.Rows.Add(ref oMissing);
                        newRow.Range.Font.Bold = 0;
                        newRow.Range.Underline = 0;
                        newRow.Range.ParagraphFormat.Alignment =
                        Word.WdParagraphAlignment.wdAlignParagraphCenter;

                        newRow.Cells[1].Range.Text = dtItems(sPO, sSup).Rows[i][3].ToString();
                        newRow.Cells[2].Range.Text = dtItems(sPO, sSup).Rows[i][4].ToString();
                        newRow.Cells[3].Range.Text = dtItems(sPO, sSup).Rows[i][8].ToString();
                        newRow.Cells[4].Range.Text = dtItems(sPO, sSup).Rows[i][10].ToString();
                        newRow.Cells[5].Range.Text = dtItems(sPO, sSup).Rows[i][11].ToString();
                    }
                    //END ROW

                    oWord.Selection.TypeParagraph();
                }

                if (fieldName == "TItems")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtTotal(sPO, sSup).Rows[0]["Unit"].ToString().Trim());
                }

                if (fieldName == "Discount")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtTotal(sPO, sSup).Rows[0]["Discount"].ToString().Trim());
                }

                if (fieldName == "TAmount")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtTotal(sPO, sSup).Rows[0]["Amount"].ToString().Trim());
                }

                if (fieldName == "Summary")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Amount"].ToString().Trim());
                }

                if (fieldName == "ReqNo")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["ReqNo"].ToString().Trim());
                }

                if (fieldName == "RevNo")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["RevNo"].ToString().Trim());
                }
            }
        }
        // End filling information in Word file

        Object oSaveAsFile = (Object)Server.MapPath("Reports/Word/tmp2.docx");
        oWordDoc.SaveAs(ref oSaveAsFile, 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);

        oWordDoc.Close(ref savechanges, ref oMissing, ref oMissing);
        oWord.Application.Quit(ref savechanges, ref oMissing, ref oMissing);

        //foreach (Process p in System.Diagnostics.Process.GetProcessesByName("winword"))
        //{
        //    try
        //    {
        //        if (p.ProcessName == "WINWORD")
        //        {
        //            if (!p.HasExited)
        //            {
        //                p.Kill();
        //                p.WaitForExit(); // possibly with a timeout
        //            }
        //        }
        //        else
        //        {
        //            lblMessage.Text = "cannot kill. try again!";
        //        }
        //    }
        //    catch (Win32Exception winException)
        //    {
        //        //process was terminating or can't be terminated - deal with it    
        //        Session["error"] = winException.Message;
        //        Response.Redirect("MessageBoard.aspx");
        //    }
        //    catch (InvalidOperationException invalidException)
        //    {
        //        //process has already exited - might be able to let this one go  
        //        Session["error"] = invalidException.Message;
        //        Response.Redirect("MessageBoard.aspx");
        //    }
        //}   

        Response.ClearContent();
        Response.ClearHeaders();
        Response.ContentType = "application/msword";
        Response.WriteFile(Server.MapPath("Reports/Word/tmp2.docx"), false);
        Response.Flush();
        Response.Close();
    }

And I followed to give the permission from Blog.Crowe.co.nz
But still got problem, I can't solve this one since last month. If you're possible, please kindly help to me. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜味拾荒者 2024-12-09 14:58:53

在服务器环境中使用完整的办公套件来执行此类流程通常是一个非常糟糕的主意。

Office 主要设计为在交互式桌面上运行的客户端应用程序。当运行服务器端时,您实际上需要一种无人看管的做事方式。

您可以获得办公服务器端组件,但我不能 100% 确定这样做的所有优点和缺点。

如果您创建 Word 2007 兼容文档,那么您可能需要考虑使用“Open XML”格式(这是 Word 2007 及以后使用的格式),Microsoft 提供了一个 Open XML SDK 来执行此类任务,

您可以在此处找到文档:http://msdn.microsoft.com/en-us/library/bb226703.aspx

如果您必须使用完整的办公套件,那么我建议您尝试手动打开 Word服务器,作为您的 Web 应用程序运行的用户。然后,您可以关闭缩写框并将其设置为不再出现,这应该(理论上)防止您看到的问题再次发生。然而,这并不意味着从那时起您将获得 100% 无问题的使用,使用 Office com 互操作会在服务器环境中带来许多问题。

有关 Microsoft 自己的方法的更多详细信息,请访问:http://support.microsoft.com/kb/257757< /a>

It's generally a very bad idea to use the full blown office suite for processes like this in a server environment.

Office is primarily designed as a client app to be run on an interactive desktop. When running server side you realistically need an untended way of doing things.

You can get office server side components, but I'm not 100% sure on all the pros and cons of doing so.

If your creating word 2007 compatible documents then you might want to look at using the "Open XML" format (Which is what word 2007 onwards uses), Microsoft provide an Open XML SDK for performing such tasks

you can find the docs here: http://msdn.microsoft.com/en-us/library/bb226703.aspx

If you must use the full blown office suite, then I could suggest that you try opening word manually on the server, as the user that your web app will run as. You can then dismiss the initals box and set it never to appear again, that should (In theory) prevent the problem your seeing from re-occurring. It does not however mean that you'll get 100% problem free use from that point on, using the Office com interop poses way to many problems in a server environment.

More details on Microsoft's own approach can be found here : http://support.microsoft.com/kb/257757

绝不放开 2024-12-09 14:58:53

简而言之,微软不支持此功能,因此不要指望它能够正常工作或可靠地工作。

长的答案是,问题在于权限以及 Word 的工作方式。您发布的链接显示了如何在 Excel 中解决此问题,但这不适用于 Word。问题是,当第一次调用 Word 时,它希望显示一个欢迎对话框,用户可以在其中输入缩写和其他内容。当它通过 IIS 运行时,不会显示该对话框,并且无法创建 COM 对象。应该有一些方法可以禁用此对话框,但微软似乎只将它们提供给批量许可证。除此之外,方法是尝试使用 IIS 内置用户帐户在服务器上运行 Word,并以这种方式摆脱首次用户对话框。

我在经典 ASP 时代完成此操作的另一种方法是使用具有自己的处理空间和用户权限的 COM+ 对象。这很有效,但我还没有在 ASP.NET 上尝试过。如果您的网络管理员不想直接向内置 IIS 授予权限,那么这是一个不错的选择。

最后一个解决方案是完全放弃使用 Word,并使用不同的方法来生成文档。这是我的方向,因为 Word 和 IIS 之间的内存使用问题不断导致负载测试期间服务器崩溃,这是不可接受的。

附加信息:

对于动态生成 W​​ord 文档的替代方法,您可以使用 RTF 模板文件。只需在文本中创建您自己的标记,您就可以在数据字段上进行查找/替换。 Word 读取/写入 RTF 文件,因此您有时只需培训用户即可。还有一些第三方工具可以创建 Word 文档,但它们价格昂贵,而且我没有使用过其中任何一个。

The short answer is that this isn't supported by Microsoft so don't expect it to work or work reliably.

The long answer is that the issue is permissions and how Word works. The link you posted shows how to get around this for Excel but this won't work for Word. The problem is that when Word is called for the first time it wants to display a welcome dialog where the user can enter initials and other stuff. When it's run via IIS, the dialog doesn't show and the COM object can't be created. There are supposed to be some ways to disable this dialog but Microsoft only seems to make them available to volume licenses. Other than that, the way to do it is to try to run Word on the server using the IIS built-in user account and get rid of the first time user dialog that way.

Another way I've done this back in the classic ASP days was to use a COM+ object with its own processing space and user permissions. This worked well but I haven't tried it with ASP.NET. This is a good option if your net admin doesn't want to give permissions to the IIS built-in directly.

The last solution is to scrap using Word entirely and use a different method to generate your documents. This is the direction I went because memory usage issues between Word and IIS kept causing server crashes during load testing which were unacceptable.

Additional Info:

As for alternative methods dynamically generating Word docs, you can use a RTF template file. Just create your own tokens in the text where you can do a find/replace on the data fields. Word reads/writes RTF files so you just have to train users sometimes. There are also third party tools available that will create Word docs but they're pricey and I haven't used any of them.

一个人的旅程 2024-12-09 14:58:53

我在 Windows 2008 64 位上使用 COM 对象的 ASP.NET 站点遇到了这个确切的错误。我最终找到的解决方案是使用regsvr32重新注册COM dll,并将IIS应用程序池的“启用32位应用程序”属性设置为true。之后效果很好。我没有使用 MS Word,所以我不能 100% 确定这对您有用,但它至少值得一试。

I had this exact error with an ASP.NET site using a COM object on Windows 2008 64-bit. The solution I finally found was to re-register the COM dll with regsvr32 and also set the IIS Application pool's "Enable 32-bit applications" property to true. It worked fine after that. I wasn't working with MS Word, so I can't say with 100% certainty that this will work for you, but it should at least be worth a try.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文