2012-10-26 36 views
2

我正在開發使用Microsoft.Office.Interop.Word在vb.net中打印Word文檔中的輸出的應用程序。如果關閉應用程序,我想關閉文檔(使用「另存爲...」對話框)。如果用戶想在應用程序結束之前關閉文檔,則應該可以。一切似乎都做工精細,但(在兩種情況下)關閉後,我得到這個錯誤:.net Word Interop關閉問題

This file is in use by another application or user.

(C:...\Templates\Normal.dotm)

我認爲問題是文檔擁有者是應用程序,使用戶無法關閉它...但是,當文檔被應用程序關閉時(使用quit方法),錯誤也會發生。 我frmMain_FormClosing運行的代碼是:

If _objWord IsNot Nothing Then 
    Try 
     _objWord.Quit() 
     System.Runtime.InteropServices.Marshal.FinalReleaseComObject(_objDoc) 
     System.Runtime.InteropServices.Marshal.FinalReleaseComObject(_objWord) 
    Catch ex As Exception 
    End Try 
End If 

我用Excel相同,我沒有任何問題。

+0

你另存爲後,你有沒有關閉該文檔? – urlreader

+0

是的,因爲我說的「另存爲」是關閉非保存文檔時出現的那個! – ufo

回答

0

當添加一個文件時,請確保您使用

Dim d As Word.Document = w.Documents.Add() 

,而不是

d = New Word.Document 'this syntax can cause a memory leak!'