1

When the user clicks on a button it opens a Word document. After the user closes the document, Word is still running in the background (confirmed by Task Manager). Is there a way to clear Word out of the memory?

This is what my code currents look like:

 Private Sub UserDocumentationToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UserDocumentationToolStripMenuItem.Click
        Dim appWord As New Word.Application
        Dim docWord As New Word.Document

        docWord = appWord.Documents.Open(Application.StartupPath & "\user_documentation1.docx", )
        appWord.Visible = True
        appWord.Activate()

    End Sub 
niton
  • 8,771
  • 21
  • 32
  • 52
  • Duplicate of [Disposing of Microsoft.Office.Interop.Word.Application](http://stackoverflow.com/questions/6777422/disposing-of-microsoft-office-interop-word-application) – Bjørn-Roger Kringsjå Apr 11 '15 at 11:01

4 Answers4

1

You need to close the Com object correctly, after calling .Quit you need the Marshall class to do that. Check Task Manager and you will see it is gone. Same for Excel as well.

Marshal.FinalReleaseComObject(appWord)
OneFineDay
  • 9,004
  • 3
  • 26
  • 37
  • 1
    Unfortunately none of these seemed to work for me. Word is still running in the background. Thanks for the help so far guys! – user3400168 Apr 11 '15 at 04:05
  • I have worked with `Word` before. This method is the only one that works? Make sure you have all previous processes closed then try this again. Quit alone will not work. – OneFineDay Apr 11 '15 at 04:09
0

You can move Dim appWord As New Word.Application outside of the sub, and then call appWord.Quit() from another sub at a later time.

S. Adam Nissley
  • 776
  • 1
  • 5
  • 13
0

Try this.

Outside method blocks:

Private appWord As Word.Application
Private docWord As Word.Document

Inside your event handler:

Private Sub UserDocumentationToolStripMenuItem_Click(sender As Object, e As EventArgs) _
Handles UserDocumentationToolStripMenuItem.Click

    Me.appWord = New Word.Application
    Me.docWord = New Word.Document

    Me.docWord = Me.appWord.Documents.Open(Application.StartupPath & 
                                           "\user_documentation1.docx", )
    Me.appWord.Visible = True
    Me.appWord.Activate()

End Sub

In your FormClosing event handler or anywhere else:

Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) _
Handles MyBase.FormClosing

    Me.appWord.Quit()

End Sub
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
0

I had a similar problem and the solution was two-fold. First, I used my own equivalent of appWord.Quit() to terminate the program. Note that unless you want the end user to be prompted whether to save changes, you will need to supply the parameter for the .Quit method. Like this: appWord.Quit(Word.WdSaveOptions.wdDoNotSaveChanges).

Second, I found a handy little Sub online for fully releasing any Word or Excel automation objects and modified it to suit my needs.

Try calling this Sub after appWord.Quit(Word.WdSaveOptions.wdDoNotSaveChanges):

Public Sub releaseObject(ByVal obj As Object)
    Try
        Dim CheckIt As Integer
        CheckIt = System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
        While CheckIt > 0
            CheckIt = System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
        End While
        obj = Nothing
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        GC.Collect()
        GC.WaitForPendingFinalizers()
    End Try
End Sub

This sub loops through to make sure all instances and objects are completely closed and removed from memory.

Hope that helps!

LegalEagle
  • 97
  • 3
  • 15