Home All Groups Group Topic Archive Search About

Cleaning up Excel After using Excel Interop

Author
18 Dec 2006 8:59 PM
Geoff
Hello

I am having an issue with my code below not closing EXCEL.EXE when I
tell it do. Any help would be appreciated.

-----

Dim excel As Microsoft.Office.Interop.Excel.Application
Dim wb As Microsoft.Office.Interop.Excel.Workbook
Dim ws As Microsoft.Office.Interop.Excel.Worksheet

excel = New Microsoft.Office.Interop.Excel.Application
wb = excel.Workbooks.Open(strDynamicFileName)
ws = wb.Sheets.Item(1)

excel.Visible = False
wb.Activate()

.... REMOVED FUNCTION CODE HERE

' Clean up
System.Runtime.InteropServices.Marshal.ReleaseComObject(ws)
ws = Nothing
wb.Close()
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb)
wb = Nothing
excel.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel)
excel = Nothing

Author
18 Dec 2006 9:09 PM
Scott M.
First, I would change the name of your Excel variable from "excel" to
something else.
Second, I'd rearrange the code to be like this:

Show quoteHide quote
> ' Clean up
> wb.Close()
> excel.Quit()

> System.Runtime.InteropServices.Marshal.ReleaseComObject(ws)
> System.Runtime.InteropServices.Marshal.ReleaseComObject(wb)
> System.Runtime.InteropServices.Marshal.ReleaseComObject(excel)

> ws = Nothing
> wb = Nothing
> excel = Nothing