khol khol - 3 months ago 13
Vb.net Question

Properly closing an office interop excel application

I'm developing a VB forms application that accesses excel sheet content using the office interop library. Developing on Windows 8 with Office 2016.

Take the following trivial example:

Import excel = Microsoft.Office.Interop.Excel

Private Sub Form_Load(sender As Object, e as EventArgs) Handles MyBase.Load
Dim excelApp as excel.Application = New excel.Application
excelApp.Quit
End Sub


This appears to cause a leak. While the form is still open, a tasklist call from the command prompt will show a running excel application (consuming 15m of memory on my machine). The excel process only terminates once the base VB application is closed by the user.

For my application, depending on the parent process to close before the excel processes are closed is unacceptable. How can I kill these excel processes?

Answer

You can use the Marshal class for that (MSDN). I found that running all excel objects in reverse order was the best -> worksheet, workbook, application.

  Marshal.FinalReleaseComObject(comObj)