Cleaning up Excel After using Excel Interop

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
 
S

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:
 

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