I'm calling a COM object from a Word-Addin.
After one point, I want to close my COM object and return to Word. My Problem is, that my COM object (my Import form of another application) keeps being opened. When I try to close it manually I get following error:
My Code:
Private Sub save_Click(sender As Object, e As RibbonControlEventArgs) Handles save.Click
Dim importer = GetObject("", "IMPORT.Application")
Dim dictionary As Dictionary(Of Integer, String)
Dim doc As Document = Globals.ThisAddIn.Application.ActiveDocument
Try
'Doing some stuff...
importer.SetWindowVisible(False)
doc.Close(False)
Catch ex As Exception
MessageBox.Show(ex.message)
Finally
GC.Collect()
GC.WaitForPendingFinalizers()
System.Runtime.InteropServices.Marshal.ReleaseComObject(importer)
importer = Nothing
End Try
End Sub
They explain everywhere on the web, that I have to use following command to release the COM object:
System.Runtime.InteropServices.Marshal.ReleaseComObject(importer)
It doesn't seem to work in my case. Does anybody know why?
Is it possible to get the processID of my COM Application, in order to kill it, in the end of my code?
// EDIT
So I tried calling myOtherMethod() in the finally statement (importer is a class variable now). I'm not doing anything else. Nothing has changed.
Private Sub myOtherMethod()
GC.WaitForPendingFinalizers()
System.Runtime.InteropServices.Marshal.ReleaseComObject(importer)
End Sub