开发者

MS Word Interop C# RPC_E_SERVERCALL_RETRYLATER Error

So I keep getting the "Application is busy" RPC_E_SERVERCALL_RETRYLATER error with the following code. It is worth noting that this exact code worked fine with Word 2003 and .doc files. After upgrading to 2007, it is no longer working. The file it getting the section count of is a ".docx" and I have made sure to use the proper version of the interop. The error occurs at a random location in the code usually.

 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;
        }

An example stacktrace:

   at Microsoft.Office.Interop.Word.DocumentClass.get_Sections()
   at MyApplication.WordMerge.split(String fileToSplit, String wher开发者_如何学CeToSave, 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()

The code is run in its own thread and no other MS Word automation code is run concurrently. Again, it worked fine before upgrading.

EDIT: When I saved the file in question as a .DOC instead of .DOCX there were no errors and the code worked fine.


IIRC, the Word COM component is an STA one, so any call into it will need to be marshaled to the thread that created it.

If by any chance the thread that created it is not pumping messages the marshaling cannot occur and you get the error you are having.


I found the solution to this. When I saved the file as a .doc instead of .docx and tried to run the code, it ran without error. For some reason having a .docx file causes massive problems with COM. I could not find any other way that worked (including using COM error handling and message pump).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜