0

I am bit of a confusion here. I let the user manually close Excel but still a process remains on the background? May you kindly help me resolve this issue. In my own understanding, I tried releasing COM objects as well with it? Also I would like to know what errors have I've been doing with it. I appreciate any help. Thank you so much.

Private Sub releaseComObject(ByVal obj As Object)
    Try
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj)
        obj = Nothing
    Catch ex As Exception
        obj = Nothing
    Finally
        GC.Collect()
    End Try
End Sub


Private Sub BtnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnExport.Click
    'verfying the datagridview having data or not
    If ((DataGridView1.Columns.Count = 0) Or (DataGridView1.Rows.Count = 0)) Then
        Exit Sub
    End If
    BtnExport.Text = "Please Wait..."
    BtnExport.Enabled = False
    'Creating dataset to export
    Dim dset As New DataSet
    'add table to dataset
    dset.Tables.Add()
    'add column to that table
    For i As Integer = 0 To DataGridView1.ColumnCount - 1
        dset.Tables(0).Columns.Add(DataGridView1.Columns(i).HeaderText)
    Next
    'add rows to the table
    Dim dr1 As DataRow
    For i As Integer = 0 To DataGridView1.RowCount - 1
        dr1 = dset.Tables(0).NewRow
        For j As Integer = 0 To DataGridView1.Columns.Count - 1
            dr1(j) = DataGridView1.Rows(i).Cells(j).Value
        Next
        dset.Tables(0).Rows.Add(dr1)
    Next
    Dim excel As New Excel.Application
    Dim wBook As Excel.Workbook
    Dim wSheet As Excel.Worksheet

    wBook = excel.Workbooks.Add()
    wSheet = CType(wBook.ActiveSheet(), Microsoft.Office.Interop.Excel.Worksheet)
    Dim dt As System.Data.DataTable = dset.Tables(0)
    Dim dc As System.Data.DataColumn
    Dim dr As System.Data.DataRow
    Dim colIndex As Integer = 0
    Dim rowIndex As Integer = 0
    For Each dc In dt.Columns
        colIndex = colIndex + 1
        excel.Cells(1, colIndex) = dc.ColumnName
    Next
    For Each dr In dt.Rows
        rowIndex = rowIndex + 1
        colIndex = 0
        For Each dc In dt.Columns
            colIndex = colIndex + 1
            excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
        Next
    Next
    wSheet.Columns.AutoFit()
    excel.Visible = True
    BtnExport.Text = "    Export"
    BtnExport.Enabled = True

    releaseComObject(wSheet)
    releaseComObject(wBook)
    releaseComObject(excel)

End Sub
  • 2
    Possible duplicate of [Excel application not quitting after calling quit](https://stackoverflow.com/questions/15697282/excel-application-not-quitting-after-calling-quit). This has been answered more than a few times here... – Trevor Sep 06 '17 at 11:52
  • Hi @Codexer Thank you for taking the time to comment but may you please enlighten me where I did wrong here? I've tried a bunch of times to release it here on my end but i seem cant find any solution. – Seth Elyon Sep 06 '17 at 12:12
  • 1
    `I've tried a bunch of times to release it here on my end`, according to the code above this clearly does not show this. Please update your post with ***all relevant*** code. – Trevor Sep 06 '17 at 13:37
  • @Codexer My apologies. I have edited my post. It's been quite awhile and I still get a process of Excel in the bg. :( – Seth Elyon Sep 06 '17 at 15:38
  • You are not `closing` some of your objects before disposing them, you need to do that. If you follow the link I provided above you would see this... – Trevor Sep 06 '17 at 16:14
  • Hey @Codexer! Just found it out, like this minute. Dang it. That took me awhile. Thank you for being patient! – Seth Elyon Sep 06 '17 at 16:18
  • Welcome, glad you got it fixed! – Trevor Sep 06 '17 at 16:27
  • I don't understand why people keep pushing that dot-rule, ReleaseComObject mumbo-jumbo as a solution. It appears to work until it doesn't. see: [https://stackoverflow.com/a/25135685/2592875](https://stackoverflow.com/a/25135685/2592875) and also my version of that answer at [https://stackoverflow.com/a/36578663/2592875](https://stackoverflow.com/a/36578663/2592875). – TnTinMn Sep 06 '17 at 20:14

0 Answers0