I have a desktop winform application that opens excel workbooks. My question is, when the user open the excel workbook, the user will be doing stuff to the workbook then save ect. At which point should I release the com object of that excel or should i leave it open? If i leave it open, the file may get stuck in the heap or whatever you call it and therefore that file cannot be open. Is this the proper way opening and closing it?
I don't have any issues with excel closing the application, i'm trying to debug a situation with excel and figuring out if it's the application opening excel that could be causing excel to become corrupt or not responding anymore
Public Shared xlsApp3 As Excel.Application
Public Shared xlsWB3 As Excel.Workbook
Public Shared Sub releaseObject(ByVal obj As Object)
Try
Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
End Try
End Sub
Public openExcel()
Dim xlsApp3 As New Excel.Application
Dim xlsWB3 As Excel.Workbook
xlsApp3.Visible = True
xlsWB3 = xlsApp3.Workbooks.Open("c:\myExcel.xlsm")
'Will it hurt to release the xlsWB3 & xlsApp3 even though the user has not close the workbook itself?
releaseObject(xlsWB3)
releaseObject(xlsApp3)
xlsWB3 = Nothing
xlsApp3 = Nothing
End Sub