I try to create a class where I want to work with an excel instance and I want to quit that excel instance on demand (as mentioned in this answer. To still be able to debug it properly I seperated the garbage colletion the following way:
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim xlsInst As New ExcelInst
GC.Collect()
GC.WaitForPendingFinalizers()
End Sub End Class
Public Class ExcelInst
Sub New()
Dim myxlApp As New Excel.Application
Dim myxlWb As Excel.Workbook
'~~> Add a new Workbook
myxlWb = myxlApp.Workbooks.Add
'~~> Display Excel
myxlApp.Visible = True
myxlApp.Quit()
End Sub End Class
Observing the task manager the excel instance quits properly as soon as the form is loaded. BUT I am not satisfied with the Class. Of cause I want to have private variables of myxlApp
and myxlWB
. But if I change my class to
Public Class ExcelInst
Private myxlApp As New Excel.Application
Private myxlWb As Excel.Workbook
Sub New()
'~~> Add a new Workbook
myxlWb = myxlApp.Workbooks.Add
'~~> Display Excel
myxlApp.Visible = True
myxlApp.Quit()
End Sub
End Class
excel doesnt close on demand anymore. Doese anyone knows why the first snippet works (according to my requirements and the second is not?