Excel does not quit after using WithEvents in the COM addin

Y

Yi

I have a COM addin for Excel written in VB.NET. In this addin, I added event
handling method for the Excel.Application object. I noticed that one Excel
process stays in the memory after I double-clicking on an Excel Worksheet
object (and then cause it to lose focus by clicking on other area of the Word
document and then close the document) that is inserted in a Word document via
Word menu "Insert/Object ->Microsoft Excel Worksheet".

The following is my testing code.
------------------------------------------------------
Private WithEvents m_Excel As Excel.Application
Public Sub OnDisconnection(ByVal RemoveMode As ...) Implements ...
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_Excel)
m_Excel = Nothing
End Sub

Public Sub OnConnection(ByVal application As Object, ...) Implements ...
m_Excel = CType(application, Excel.Application)
End Sub

Private Sub m_Excel_WorkbookOpen(ByVal Wb As
Microsoft.Office.Interop.Excel.Workbook) Handles m_excel.WorkbookOpen
End Sub
------------------------------------------------------

Notice that if I remove the method "m_Excel_WorkbookOpen", the above
operation will not leave a hanging Excel process. So, I guess the use of
Event handling in this code caused the problem. Is there a solution to this
issue?

Many thanks.
 
M

Mike Walker [MVP]

Hi

You need to release your objects for the Excel application to fire your
ondisconnection process. On your class you need to implement IDispose and
then release the objects in the finaliser to make the Excel application quit
succesfully.

Regards

Mike Walker MVP
Visual Developer VSTO
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top