2013-06-04 98 views
0

我有一種情況,無論何時在Sharepoint庫中保存新文檔(新文檔>另存爲)時,自定義事件處理程序(ItemAdded)都會在文檔記錄上觸發更新並且填充一些列值。此外,該文檔以編程方式簽入。由於它在後端觸發,這些更改從字界面中看不到。我需要關閉並重新打開服務器上的文檔,以便它以所有服務器屬性和正確的檢出狀態打開。我正在使用以下代碼關閉並打開:Word應用程序關閉並重新打開

Word._Document doc = this.Application.ActiveDocument; 
    doc.Close(Word.WdSaveOptions.wdDoNotSaveChanges, System.Type.Missing, System.Type.Missing); 
    Word.Document aDoc = this.Application.Documents.Open(docPath, System.Type.Missing, true, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, true, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing); 
    aDoc.Activate(); 

文檔關閉正常,但應用程序在打開時崩潰。但是,如果我只關閉代碼中的文檔並手動從最近的文件列表中打開文檔,則文檔可以正常打開。在打開文檔之前,我需要做些清理工作嗎?

請注意,我使用DocumentBeforeSave事件(Cancel = true)覆蓋了單詞的默認保存操作。不知道這是否是相關的信息。

編輯:我試圖捕捉,如果上述塊thows任何COMException或任何種類的異常,但它不。它只是在公開聲明中崩潰。

回答

0

嘗試使用以下內容:

 (new Thread(() => 
     { 
      doc.Close(Word.WdSaveOptions.wdDoNotSaveChanges, System.Type.Missing, System.Type.Missing); 

     })).Start(); 
相關問題