2

I have the following snippet of code. It works (opens all the Word documents in a directory and then closes them down)...but it doesn't clean up after itself when I totally exit the program.

By this I mean that if I look at the TaskManager once I exit the VB.NET application I see the WINWORD.EXE even though it did not exist before I opened the application.

Here's the declarations I have:

Dim WordApp As Microsoft.Office.Interop.Word.Application
Dim aDoc As Microsoft.Office.Interop.Word.Document

Dim missing As Object = System.Reflection.Missing.Value
Dim nullobj As Object = System.Reflection.Missing.Value

Dim MYreadOnly As Object = False
Dim isVisible As Object = False

And here's the code:

Private Sub cmdGenerate_Click(sender As System.Object, e As System.EventArgs) Handles cmdGenerateKeywords.Click

  Dim xmldoc As New XmlDataDocument()
  Dim xmlnode As XmlNodeList
  Dim i As Integer
  Dim fs As FileStream

  WordApp = New Microsoft.Office.Interop.Word.Application
  WordApp.Visible = False

  For Each f As FileInfo In New DirectoryInfo(txtFolderName.Text).GetFiles("*.docx")
    ' Open the document that was chosen by the dialog
    aDoc = WordApp.Documents.Open(f.FullName, missing, [MYreadOnly], _
           missing, missing, missing, missing, missing, missing, missing, _
           missing, isVisible)
    'aDoc.Close()
    aDoc = Nothing
  Next

  'Close the Word Document
  'aDoc.Close(nullobj, nullobj, nullobj)
  WordApp.Application.Quit()
  WordApp = Nothing
End Sub

As you can tell I've commented and uncommented various statements in regards to closing down Word documents and the Word Application itself. Nothing I have tried seems to be able to get rid of that pesky WINWORD.EXE

Something seems to have a lock and will not let it close down? Is that it?

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
user737058
  • 69
  • 1
  • 5
  • Don't stop. Keep running your code. If you have no more useful code to run then end your application. Word quits when the garbage collector runs. Otherwise endlessly covered by questions about Marshal.ReleaseComObject(). – Hans Passant Feb 27 '13 at 23:59
  • @HansPassant: it's evening here. I read your comment as `Don't stop. Keep debugging...`. :) – Victor Zakharov Feb 28 '13 at 00:58
  • Possible duplicate: http://stackoverflow.com/q/2511464 – Robert Harvey Feb 28 '13 at 00:59

2 Answers2

2

Run explicitly Garbage Collector, as is shown in this article:

 // Clean up the unmanaged Word COM resources by forcing a garbage 
 // collection as soon as the calling function is off the stack (at 
 // which point these objects are no longer rooted).

 GC.Collect();
 GC.WaitForPendingFinalizers();
 // GC needs to be called twice in order to get the Finalizers called 
 // - the first time in, it simply makes a list of what is to be 
 // finalized, the second time in, it actually is finalizing. Only 
 // then will the object do its automatic ReleaseComObject.
 GC.Collect();
 GC.WaitForPendingFinalizers();

Despite link above, my experience is that run once is enough. But second call doesn't throw an error, so do it this way.

Roman Plischke
  • 1,042
  • 7
  • 14
0
fs.Close()
fs.Dispose()

http://msdn.microsoft.com/en-us/library/system.io.filestream.dispose.aspx

This releases your resources. Might allow other close methods to work. I read through the code twice and do not see you using "fs" anywhere like I would expect.

ZL1Corvette
  • 415
  • 1
  • 6
  • 24