2012-10-26 156 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!'