View Single Post
 
Old 05-16-2015, 06:21 PM
WFaz WFaz is offline
Newcomer
 
Join Date: May 2015
Posts: 1
Default COM Errors

I have managed to get Excel to disappear from the task manager in most cases. I use late binding to insure compatibility with the user's installed version of Excel no matter what version I have installed (2010). I avoid all double dot references by creating named variables for every workbook, worksheet and range. I call GC.Collect and wait for pending finalizers twice, and then specifically use FinalReleaseComObject for every object.
My only problem is that I cannot figure out how to kill Excel if a Com Error occurs in my code. If an error is thrown, Excel will disappear from the task list after my application is stopped, but not after the user closes Excel. Any ideas on how to solve this last problem?
Thanks, Bill

Here is the typical code structure ...

Try

oKxl = CreateObject("Excel.Application")
oBooks = oKxl.Workbooks
oKwb = oBooks.Open(strDataPath & strFileName, , True)
oSheets = oKwb.Worksheets
oKws = oSheets.Item(1)
oKxl.DisplayAlerts = False
oKxl.AlertBeforeOverwriting = False
oKxl.ScreenUpdating = False
oKxl.Visible = False
oKws.EnableCalculation = False

DO SOME STUFF HERE

oKws.PageSetup.PrintArea = "A1:N" & CStr(xlRow - 1)
oKxl.ScreenUpdating = True
oKws.EnableCalculation = True
oKxl.Calculate()
oKxl.Visible = True
oKxl.UserControl = True

Catch Ex As Exception
MessageBox.Show("Appropriate Error Message")
If oKxl IsNot Nothing Then
oKxl.Quit()
End If

Finally
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oKws)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oSheets)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oKwb)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oBooks)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oKxl)

End Try

Last edited by WFaz; 05-16-2015 at 06:37 PM. Reason: Add some code
Reply With Quote