Excel.Application.Quit leaving Excel process stays active

G

Guest

Well here is another struggling developer!

The example below shows some code that works - case 1 and 5 and some code
that does not - case 2,3 and 4. Why do case 2 to 4 not work and what is the
workaround?

Private Sub NAR(ByVal o As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
Catch
Finally
o = Nothing
End Try
End Sub
Private Sub TestXLQuit(ByVal XLFile As String, ByVal myCase As Integer)
Dim myExcelApp As New Microsoft.Office.Interop.Excel.Application
Dim myWkbks As Microsoft.Office.Interop.Excel.Workbooks
Dim myWkb As Microsoft.Office.Interop.Excel.Workbook
Dim mySheet As Microsoft.Office.Interop.Excel.Worksheet
Try
myWkbks = myExcelApp.Workbooks
myWkbks.Open(XLFile)
myWkb = myWkbks(1)

Dim w As Integer

Select Case myCase
Case 1
'application closes as expected.
Case 2
'application does not close.
w = myWkb.Worksheets.Count
Case 3
'application does not close.
mySheet = myWkb.Worksheets.Item("Sheet1")
w = mySheet.Cells(10, 10).value
Case 4
'application does not close.
mySheet = myExcelApp.ActiveSheet
w = mySheet.Cells(10, 10).value
Case 5
'application closes as expected.
mySheet = myExcelApp.ActiveSheet
End Select


NAR(mySheet)
mySheet = Nothing
myWkb.Close(True)
NAR(myWkb)
myWkb = Nothing
NAR(myWkbks)
myWkbks = Nothing
myExcelApp.Quit()
NAR(myExcelApp)
myExcelApp = Nothing

Catch ex As Exception
Response.Write(ex.ToString)
End Try

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top