Avatar of spen_lang
spen_lang
 asked on

Excel Process Not Closing after Using "Microsoft.Office.Interop"

Hi,

I am using the following code to open an existing Excel file and saving it as different format.

However the Excel.exe process is not closing...

  

Imports Microsoft.Office.Interop
      
Dim strPath As String
        Dim strFile As String
        Dim strExtension As String
        Dim strPathFile As String

        strPath = "c:\test\"
        strFile = "test"
        strExtension = ".xls"
        strPathFile = strPath + strFile + strExtension



        Try

            Dim xlApp = New Excel.Application
            xlApp.Application.DisplayAlerts = False
            Dim xlWorkBooks As Excel.Workbooks = xlApp.Workbooks
            Dim xlWorkBook As Excel.Workbook = xlWorkBooks.Open(strPathFile, System.Reflection.Missing.Value, System.Reflection.Missing.Value, _
                                                  System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, _
                                                  System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, _
                                                  System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, _
                                                  System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value)
            Dim xlSheets As Excel.Sheets = xlWorkBook.Sheets
            Dim xlWorkSheet As Excel.Worksheet = xlSheets(1)

            strFile = "test2"
            strPathFile = strPath + strFile + strExtension

            xlWorkSheet.SaveAs(strPathFile, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8, Type.Missing, Type.Missing, False, False, _
                              Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, _
                              Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing)

            MsgBox("open")
            xlWorkBook.Close()
            xlWorkBooks.Close()
            xlApp.Quit()

            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet)
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets)
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook)
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBooks)
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)

        Catch ex As Exception
            MsgBox("error")
        End Try

Open in new window

Programming.NET ProgrammingVisual Basic.NET

Avatar of undefined
Last Comment
joriszwaenepoel

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
joriszwaenepoel

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
spen_lang

ASKER
That is great thanks. Do you know how I would specify what sheet to open?
joriszwaenepoel

I think you can use something like

Dim sheet = xlWorkBook.Sheets("mysheetname")

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23