MS Word互操作C#RPC#E_SERVERCALL_RETRYLATER错误

MS Word互操作C#RPC#E_SERVERCALL_RETRYLATER错误,c#,.net,com,office-interop,C#,.net,Com,Office Interop,因此,我不断收到“应用程序正忙”RPC_E_SERVERCALL_RETRYLATER错误,代码如下。值得注意的是,这段代码在Word 2003和.doc文件中运行良好。升级到2007年后,它不再工作。它获取节计数的文件是一个“.docx”,我已确保使用正确版本的互操作。错误通常发生在代码中的随机位置 public int GetSectionsCount(string fileName) { wrdApp = new Application();

因此,我不断收到“应用程序正忙”RPC_E_SERVERCALL_RETRYLATER错误,代码如下。值得注意的是,这段代码在Word 2003和.doc文件中运行良好。升级到2007年后,它不再工作。它获取节计数的文件是一个“.docx”,我已确保使用正确版本的互操作。错误通常发生在代码中的随机位置

 public int GetSectionsCount(string fileName) {
            wrdApp = new Application();
            Object file = fileName;
            Documents docs = wrdApp.Documents;
            wrdDoc = docs.Open(ref file, 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);
            int count = wrdDoc.Sections.Count;
            wrdDoc.Close(ref oFalse, ref oMissing, ref oMissing);
            wrdApp.Quit(ref oFalse, ref oMissing, ref oMissing);
            Marshal.ReleaseComObject(docs);
            Marshal.ReleaseComObject(wrdDoc);
            Marshal.ReleaseComObject(wrdApp);
            wrdDoc = null;
            wrdApp = null;
            return count;
        }
stacktrace示例:

   at Microsoft.Office.Interop.Word.DocumentClass.get_Sections()
   at MyApplication.WordMerge.split(String fileToSplit, String whereToSave, String quarterExtension, Form1 pb) in\\Projects\\MyApplication\\WordMerge.cs:line 176
   at MyApplication.PMLettersManager.DoSplits() in \\Projects\\PyForms3\\PMLettersManager.cs:line 179
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
该代码在自己的线程中运行,没有其他MS Word自动化代码同时运行。同样,它在升级之前运行良好


编辑:当我将有问题的文件保存为.DOC而不是.DOCX时,没有错误,代码工作正常。

IIRC,Word COM组件是STA组件,因此对它的任何调用都需要封送到创建它的线程


如果创建它的线程没有泵送消息,封送处理将无法发生,并且您将得到当前的错误。

我找到了解决方案。当我将文件保存为.doc而不是.docx并尝试运行代码时,它运行时没有出错。由于某些原因,使用.docx文件会导致COM出现大量问题。我找不到任何其他有效的方法(包括使用COM错误处理和消息泵)。

Word 2003运行良好——这会影响任何事情吗?当线程混合在一起时,问题可能已经存在,但没有任何表现,现在由于一个简单的更改,您遇到了竞争条件。我会尝试删除线程以验证错误是否仍然存在。我刚刚发现,当我将相关文件保存为.DOC而不是.DOCX时,没有错误,代码运行良好。。。这是一种奇怪的尝试,让Word应用程序可见,以检查它是否被某种模式对话框阻止。