2
votes

I'm developing an application in vb.net that use Microsoft.Office.Interop.Word for printing an output in a Word document. If I close the application I want to close the doc (with the "Save As..." dialog). If the user wants to close the doc before the application ends this should be possible. Everything seems work fine, but after the closing (in both the case) I get this error:

This file is in use by another application or user.

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

I supposed the problem is that the doc owner is the application so the user can't close it... But the error also happens when the doc is closed by the application (with the quit method). The code I run in frmMain_FormClosing is:

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

I do the same with Excel and I don't have any problem.

1
after you SaveAs, did you close the doc? - urlreader
Yes, because the "SaveAs" I'm talking about is the one that appears when you close a non saved doc! - ufo

1 Answers

0
votes

When adding a document, make sure you use

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

and not

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