2011-10-16 91 views
0

所以我不斷收到「應用程序正忙」RPC_E_SERVERCALL_RETRYLATER錯誤,下面的代碼。值得注意的是,這個確切的代碼在Word 2003和.doc文件中運行良好。升級到2007年後,它不再有效。它獲得部分數的文件是「.docx」,我已經確保使用正確版本的interop。錯誤通常發生在代碼中的隨機位置。MS Word互操作C#RPC_E_SERVERCALL_RETRYLATER錯誤

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

一個例子堆棧跟蹤:

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有沒有錯誤和代碼工作正常。

回答

0

我找到了解決方案。當我將文件保存爲.doc而不是.docx並嘗試運行代碼時,它運行時沒有錯誤。出於某種原因,具有.docx文件會導致COM出現嚴重問題。我找不到任何其他工作方式(包括使用COM錯誤處理和消息泵)。

0

IIRC,Word COM組件是一個STA,因此任何對它的調用都需要封送到創建它的線程。

如果有任何機會創建它的線程不是泵送消息,封送處理不會發生,並且您會得到您遇到的錯誤。

+0

它與Word 2003工作正常 - 這會影響什麼? – jle

+0

線程混合時,問題可能已經存在,但沒有任何表現,現在由於簡單的更改,您正在碰撞競賽狀態。我會嘗試刪除線程以驗證錯誤是否仍然存在。 –

+0

錯誤仍然發生... – jle