Cleaning up Excel After using Excel Interop

  • Thread starter Thread starter Geoff
  • Start date Start date
G

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
 
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:
 
Back
Top