Question : Excel Interop, VB.Net, process can't be destroyed

In the attached code, in spite of the .Quit method being called, the Excel process does not end until I exit the application.

How can I force this process to close?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
Public Shared Sub Export(ByVal f As Form1)
        If f.dgvRec.Rows.Count > 0 Then
            Try
                f.Cursor = Cursors.WaitCursor

                Dim xla As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application()

                Dim wb As Microsoft.Office.Interop.Excel.Workbook = xla.Workbooks.Add(Microsoft.Office.Interop.Excel.XlSheetType.xlWorksheet)

                Dim ws As Microsoft.Office.Interop.Excel.Worksheet = DirectCast(xla.ActiveSheet, Microsoft.Office.Interop.Excel.Worksheet)

                ws.Cells(1, 1) = "Selected"
                ws.Cells(1, 2) = "PO"
                ws.Cells(1, 3) = "AG Item #"
                ws.Cells(1, 4) = "Description"
                ws.Cells(1, 5) = "Cus/Loc"
                ws.Cells(1, 6) = "Vend Item #"
                ws.Cells(1, 7) = "Qty Ordered"
                ws.Cells(1, 8) = "Qty Received"
                ws.Cells(1, 9) = "Qty Rejected"
                ws.Cells(1, 10) = "Reason Code"
                ws.Cells(1, 11) = "Qty Rem"
                ws.Cells(1, 12) = "Unit Cost"
                ws.Cells(1, 13) = "Rcv Value"
                ws.Cells(1, 14) = "Vend Qty Inv"
                ws.Cells(1, 15) = "Vend Unit Cost"
                ws.Cells(1, 16) = "Vend Value"
                ws.Cells(1, 17) = "Qty Var"
                ws.Cells(1, 18) = "Dollar Var"
                ws.Cells(1, 19) = "Off Inv"
                ws.Cells(1, 20) = "Receiver"
                ws.Cells(1, 21) = "Date"
                ws.Cells(1, 22) = "Voucher"

                For i As Integer = 0 To f.dgvRec.Rows.Count - 1
                    Dim dr As DataGridViewRow = f.dgvRec.Rows(i)

                    For j As Integer = 0 To 21
                        ws.Cells(i + 3, j + 1) = dr.Cells(j).Value.ToString()
                        Select Case j + 1
                            Case 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19
                        End Select
                    Next 'j
                Next 'i

                f.sfd.InitialDirectory = "c:\"
                f.sfd.Filter = "Excel files (*.xls)|*.xls|Excel files (xlsx.*)|*.xlsx"
                f.sfd.CreatePrompt = True
                f.sfd.OverwritePrompt = True
                f.sfd.Title = "Save Grid Worksheet"

                f.sfd.ShowDialog(f)

                If f.sfd.FileName.Trim.Length <> 0 Then
                    wb.SaveAs(f.sfd.FileName)
                End If

                wb.Close()
                xla.Quit()

            Catch ex As Exception
                Throw New Exception(ex.Message)
            Finally
                f.Cursor = Cursors.Default
            End Try


        End If

    End Sub

Answer : Excel Interop, VB.Net, process can't be destroyed

Well in your case, you shouldn't need "Dim ExcelClass As Microsoft.Office.Interop.Excel.ApplicationClass" since you are creating xla as New Excel.Application.  Also you won't need the ReleaseComObject code anymore (Actions.NAR()) or the GC.  Your Excel code is just a bit different from mine, but try this.  All you need to do is pass the " PostMessage" your Excel application object.

 

PostMessage(xla.Hwnd, WM_QUIT, 0, 0)


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
Dim xla As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application()

                Dim wb As Microsoft.Office.Interop.Excel.Workbook = xla.Workbooks.Add(Microsoft.Office.Interop.Excel.XlSheetType.xlWorksheet)

                Dim ws As Microsoft.Office.Interop.Excel.Worksheet = DirectCast(xla.ActiveSheet, Microsoft.Office.Interop.Excel.Worksheet)

               'blah, blah, blah
                wb.Close()
                xla.Quit()

                ws = Nothing
                wb = Nothing

                PostMessage(xla.Hwnd, WM_QUIT, 0, 0)

                xla = Nothing
Random Solutions  
 
programming4us programming4us