Cleaning up Excel After using Excel Interop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Geoff

    #1

    Cleaning up Excel After using Excel Interop

    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.Offic e.Interop.Excel .Application
    Dim wb As Microsoft.Offic e.Interop.Excel .Workbook
    Dim ws As Microsoft.Offic e.Interop.Excel .Worksheet

    excel = New Microsoft.Offic e.Interop.Excel .Application
    wb = excel.Workbooks .Open(strDynami cFileName)
    ws = wb.Sheets.Item( 1)

    excel.Visible = False
    wb.Activate()

    .... REMOVED FUNCTION CODE HERE

    ' Clean up
    System.Runtime. InteropServices .Marshal.Releas eComObject(ws)
    ws = Nothing
    wb.Close()
    System.Runtime. InteropServices .Marshal.Releas eComObject(wb)
    wb = Nothing
    excel.Quit()
    System.Runtime. InteropServices .Marshal.Releas eComObject(exce l)
    excel = Nothing

  • Scott M.

    #2
    Re: Cleaning up Excel After using Excel Interop

    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:
    ' Clean up
    wb.Close()
    excel.Quit()
    System.Runtime. InteropServices .Marshal.Releas eComObject(ws)
    System.Runtime. InteropServices .Marshal.Releas eComObject(wb)
    System.Runtime. InteropServices .Marshal.Releas eComObject(exce l)
    ws = Nothing
    wb = Nothing
    excel = Nothing

    Comment

    Working...