ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Excel.Application.Quit leaving Excel process stays active (https://www.excelbanter.com/excel-programming/319280-excel-application-quit-leaving-excel-process-stays-active.html)

Siggy

Excel.Application.Quit leaving Excel process stays active
 
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.ReleaseComO bject(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



--
www.gandalfsolutions.co.nz

Bob Phillips[_7_]

Excel.Application.Quit leaving Excel process stays active
 
Don't have .net, so untried, but add

myWkb.Saved = True

perhaps?


--
HTH

-------

Bob Phillips
"Siggy" wrote in message
...
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.ReleaseComO bject(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



--
www.gandalfsolutions.co.nz





All times are GMT +1. The time now is 03:33 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com