1

I've ran into the issue of floating excel.exe files even after closing the workbooks and quitting the excel application. After closing, I release the worksheet, workbook, and application objects. Am I missing an object to release?

The code below opens up my excel file and compares specific cells with the users selected item in the window forms combobox.

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbTest.SelectedIndexChanged
    FolderBrowserDialog1.SelectedPath = "K:\ETL Test Files\" & Main2.portalInst.txtYear.Text & "\" & Main2.portalInst.txtCompany.Text & "\" & Main2.portalInst.txtReport.Text & "\Test Data\Test Results-1.xlsx"
    Dim rows As Integer = 2
    Dim excel_app As New Microsoft.Office.Interop.Excel.Application
    excel_app.Visible = True
    Dim workbook As excel.Workbook = excel_app.Workbooks.Open(FolderBrowserDialog1.SelectedPath) 'each test data folder will have this file
    Dim worksheet As excel.Worksheet = workbook.Worksheets("Sheet1")
    While (worksheet.Cells.Range("A" & rows).Value IsNot Nothing)
        rows = rows + 1
    End While
    If cmbTest.SelectedIndex <> -1 Then
        txtCategory.Enabled = True
        Button2.Enabled = True
        Button1.Enabled = True
        grpComp.Enabled = True
        grpSuscep.Enabled = True
        For i As Integer = 2 To rows Step 1
            If worksheet.Cells.Range("A" & i).Value IsNot Nothing Then
                If worksheet.Cells.Range("A" & i).Value.ToString & "-" & worksheet.Cells.Range("D" & i).Value.ToString = cmbTest.SelectedItem.ToString Then
                    txtCategory.Text = worksheet.Cells.Range("D" & i).Value.ToString
                End If
            End If
        Next
    End If
    workbook.Close()
    excel_app.Quit()
    ReleaseObject(worksheet)
    ReleaseObject(workbook)
    ReleaseObject(excel_app)
End Sub

Private Sub ReleaseObject(ByVal obj As Object)
    Try
        Dim intRel As Integer = 0
        Do
            intRel = System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
        Loop While intRel > 0
        MsgBox("Final Released obj # " & intRel)
    Catch ex As Exception
        MsgBox("Error releasing object" & ex.ToString)
        obj = Nothing
    Finally
        GC.Collect()
    End Try
End Sub

Any help is appreciated. Thanks in advance!

-Rueben Ramirez

fotinakis
  • 7,792
  • 2
  • 26
  • 28

0 Answers0