-1

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?

khol
  • 276
  • 3
  • 9

1 Answers1

1

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)
OneFineDay
  • 9,004
  • 3
  • 26
  • 37
  • Could you please explain why the marshaling is needed and why the reverse order? This will help the OP understand his issue... – Trevor Aug 21 '16 at 19:11
  • When I was using excel as com object I was finding releasing there process was difficult. After much experimenting and some guidance from some others in forums - this was found and told to me to be a best practice for these objects. The reverse order was helpful - I don't remember the exact reason - much time and beer have occurred. MS built this function just for this. – OneFineDay Aug 21 '16 at 19:15
  • 2
    Check out this article by Sidharth [**here**](http://www.siddharthrout.com/2012/08/06/vb-net-two-dot-rule-when-working-with-office-applications-2/) which explains this. Very good when working with COM objects... The first paragraph explains why... – Trevor Aug 21 '16 at 19:21
  • @Zaggler, thanks for that article. – OneFineDay Aug 21 '16 at 21:26