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 t
this is a perfect solution, i had same problem, i just followed this one and it is working perfect.
object objFalse = false;
wordApplication.Quit(ref objFalse, ref objFalse, ref objFalse);
does "oDocuments" have a .Dispose() or .Close() method? you're disposing of the other 2, but not this one.
Try calling GC.WaitForPendingFinalizers()
and using Marshal.FinalReleaseComObject
instead of Marshal.ReleaseComObject
. This gets rid of the need to loop it.
Update your code to this and try it (the GC calls are in the beginning on purpose):
GC.Collect()
GC.WaitForPendingFinalizers()
oDoc.Close()
Marshal.FinalReleaseComObject(oDoc)
Marshal.FinalReleaseComObject(oDocuments)
oWord.Quit()
Marshal.FinalReleaseComObject(oWord)
You might also want to check out this related question discussing the issue for Excel.
I came across your post because of a similar issue with the template. I would get a message prompting me to save the .dotm file whenever I would try to close word in my program. I couldn't use your accepted answer because I don't have an exact template path, I just open whatever document the program receives.
what I used is
Word.NormalTemplate.Saved = true;
when I used that code before I disposed of the application, it would no longer bring up the dialog saying I hadn't saved the template, and it would run the disposal without leaving the unwanted "winWord.exe" process running.
I got the "NormalTemplate.Saved" tip from the user "NeedSomeAnswers" on the visual basic forums here. In his words "[it] doesn't actually save to the Normal, it just tells Word that the Normal has already been saved so it doesn't need to save it".
I think this is a second answer to the same problem. I hope it helps.
Have an awesome day, and be well.
-any day your code works is a good day to celebrate-
Have you tried changing
oWord.Visible = False
to
oWord.Visible = True
?
I ask because Word may be asking you to do something that's related to this template you are trying to use. If it thinks there's a dialog showing, it will normally not shut down. IIRC, there's a way to do Quit so that it forces Quit and won't wait on any dialogs. But, it's been a while.
Although this is C# but maybe it will help you out. I'm using this method to merge multiple documents into one. I pass all documents in Arraylist, and Word seems to close properly when done.
public static void documentsMerge(object fileName, ArrayList arrayList) {
// object fileName = Path.Combine(Environment.CurrentDirectory, @"NewDocument.doc");
File.Delete(fileName.ToString());
try {
wordApplication = new ApplicationClass();
var doc = wordApplication.Documents.Add(ref missing, ref missing, ref missing, ref missing);
try {
doc.Activate();
int count = 0;
foreach (var alItem in arrayList) {
addDocument(alItem, doc, count == 0);
count++;
}
// addDocument(@"D:\Projects\WordTests\ConsoleApplication1\Documents\Doc1.doc", doc ) ; //, false);
// addDocument(@"D:\Projects\WordTests\ConsoleApplication1\Documents\Doc2.doc", doc ) ; //, true);
doc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
} finally {
doc.Close(ref missing, ref missing, ref missing);
}
} finally {
wordApplication.Quit(ref missing, ref missing, ref missing);
}
}
The finally block is useful for cleaning up
any resources allocated in the try block as well as running any code that must execute even if there is an exception. Control is always passed to the finally block regardless of how the try block exits.
So try to put your code into try / finally block and see how it behaves then?
For VB.NET
Try
' Statement which can cause an exception.
Catch x As Type
' Statements for handling the exception
Finally
End Try 'Any cleanup code