在测试服务器上自动化 Word 时出错

发布于 2024-09-24 07:22:16 字数 2685 浏览 3 评论 0原文

我正在尝试从 .Net Web 应用程序打开 Word 文档。代码(简而言之)包含...

using Word = Microsoft.Office.Interop.Word;
using System.Reflection;

并且,实际创建文档的代码包括:

object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\\endofdoc"; // \endofdoc is a predefined bookmark 
object oTemplate = WebConfigurationManager.AppSettings["GuidelineRepsTemplate"]; //this points to a .doc I am using as a template

//Start Word and create a new document.
    Word._Application oWord;
    Word._Document oDoc;
    oWord = new Word.Application();
    oWord.Visible = true;
    oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing, ref oMissing, ref oMissing);

    object oBookMark = "Representative";
    oDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = lblRecipient.Text;

    object oBookMark1 = "Guidelines";
    oDoc.Bookmarks.get_Item(ref oBookMark1).Range.Text = Guidelines.ToString();

object fileName = WebConfigurationManager.AppSettings["GuidelineRepsPath"] + ContactID.ToString() + "\\" + fileString2 + ".doc";
        object FileFormat = Word.WdSaveFormat.wdFormatDocument;
        oDoc.SaveAs(ref fileName, ref FileFormat, 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);

我用作模板的 Word 文档中有两个书签,我将这些书签填充到上面的代码中。

在我的开发盒上一切正常。该文档在 Word 中打开,用户可以根据需要进行更改,并将其保存在我为其创建的目录中(上面未显示目录代码)。

当我发布到测试服务器时 - 每次运行它时,我都会收到此错误:

The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER)) 

堆栈跟踪无法让它记住出了什么问题 - 一个例子是:

[COMException (0x8001010a): The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER))]
Microsoft.Office.Interop.Word.DocumentClass.get_Bookmarks() +0
SendConfirmEmailReps.CreateDocs(Int32 ContactID, Int32 OrganisationID) +942
SendConfirmEmailReps.Page_Load(Object sender, EventArgs e) +317
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +50
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627

这表明书签有问题。因此,我取出了对书签的引用,并开始使用名为 Hello.doc 的文件作为我的模板。没有书签——只有“你好”这个词。

这也失败了 - 但 StackTrace 表明这是在调用 SaveAs 时发生的。

所以,问题是......为什么它可以在我的开发盒上运行,但不能在服务器上运行。服务器上安装有 Word。

I am trying to open a Word document from a .Net web application. The code (in brief) contains ...

using Word = Microsoft.Office.Interop.Word;
using System.Reflection;

and, the code that actually creates the document includes:

object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\\endofdoc"; // \endofdoc is a predefined bookmark 
object oTemplate = WebConfigurationManager.AppSettings["GuidelineRepsTemplate"]; //this points to a .doc I am using as a template

//Start Word and create a new document.
    Word._Application oWord;
    Word._Document oDoc;
    oWord = new Word.Application();
    oWord.Visible = true;
    oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing, ref oMissing, ref oMissing);

    object oBookMark = "Representative";
    oDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = lblRecipient.Text;

    object oBookMark1 = "Guidelines";
    oDoc.Bookmarks.get_Item(ref oBookMark1).Range.Text = Guidelines.ToString();

object fileName = WebConfigurationManager.AppSettings["GuidelineRepsPath"] + ContactID.ToString() + "\\" + fileString2 + ".doc";
        object FileFormat = Word.WdSaveFormat.wdFormatDocument;
        oDoc.SaveAs(ref fileName, ref FileFormat, 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);

The Word document I use as a template has two bookmarks in it and I populate these bookmarks within the code above.

On my development box it all works fine. The document opens in Word, users can make changes if they wish and it saves in the directory I have created for it (directory code not shown above).

When I publish to a test server - every time I run it I get this error:

The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER)) 

The stack trace can't make it's mind up what is wrong - one example is:

[COMException (0x8001010a): The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER))]
Microsoft.Office.Interop.Word.DocumentClass.get_Bookmarks() +0
SendConfirmEmailReps.CreateDocs(Int32 ContactID, Int32 OrganisationID) +942
SendConfirmEmailReps.Page_Load(Object sender, EventArgs e) +317
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +50
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627

which suggests something wrong with the bookmarks. So I took references to the bookmarks out and started using a file called Hello.doc as my template. No bookmarks - just the word 'hello' in it.

This fell over too - but the StackTrace indicated it was when SaveAs was called.

So, the question is ... why does it work on my development box but not on the server. The server has Word installed on it.

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

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

发布评论

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

评论(1

软糖 2024-10-01 07:22:16

我认为这就是微软Office自动化服务器端无人值守使用所固有的稳定性问题。 此处对此进行了更详细的描述。

I think this is the stability problem inherent in server-side unattended usage of Microsoft Office automation. It's described in more detail here.

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