Using Word COM objects in .NET, WinWord process won't quit after calling Word.Documents.Add
Posted
by Keith
on Stack Overflow
See other posts from Stack Overflow
or by Keith
Published on 2010-03-24T21:15:27Z
Indexed on
2010/03/24
21:23 UTC
Read the original article
Hit count: 1205
I'm running into the classic scenario where, when creating Word COM objects in .NET (via the Microsoft.Office.Interop.Word assembly), the WinWord process won't exit even though I'm properly closing and releasing the objects.
I've narrowed it down to the use of the Word.Documents.Add() method. I can work with Word in other ways without a problem (opening documents, modifying contents, etc) and WinWord.exe quits when I tell it to. It's once I use the Add() method that the process is left running.
Here is a simple example which reproduces the problem:
Dim oWord As New Word.Application()
oWord.Visible = FalseDim oDocuments As Word.Documents = oWord.Documents
Dim oDoc As Word.Document = oDocuments.Add(Template:=CObj(sTemplatePath), NewTemplate:=False, DocumentType:=Word.WdNewDocumentType.wdNewBlankDocument, Visible:=False)
' dispose objects
oDoc.Close()
While (Marshal.ReleaseComObject(oDoc) <> 0)
End While
oDoc = NothingoWord.Quit()
While (Marshal.ReleaseComObject(oWord) <> 0)
End While
oWord = Nothing
As you can see I'm creating and disposing the objects properly, even taking the extra step to loop Marsha.ReleaseComObject until it returns the proper code. Working with the Word objects is fine in other regards, it's just that pesky Documents.Add that is causing me grief. Is there another object that gets created in this process that I need to reference and dispose of? Is there another disposal step I need to follow? Something else? Your help is much appreciated :)
© Stack Overflow or respective owner