You should never call Marshal.ReleaseComObject() - the best approach is to call the .NET garbage collector to clean up by calling GC.Collect() .
You must be careful that the code that speaks to Excel is not the same as your GC.Collect() , otherwise the debugger might keep the objects alive longer than you expected.
The big picture:
Sub WrapperThatCleansUp() ' NOTE: Don't call Excel objects in here... ' Debugger would keep alive until end, preventing GC cleanup ' Call a separate function that talks to Excel DoTheWork() ' Now Let the GC clean up (twice, to clean up cycles too) GC.Collect() GC.WaitForPendingFinalizers() GC.Collect() GC.WaitForPendingFinalizers() End Sub Sub DoTheWork() Dim xls As New Excel.Application Dim book As Excel.Workbook Dim oSheet As Excel.Worksheet xls.Workbooks.Open("Test.xlsx") book = xls.ActiveWorkbook oSheet = book.ActiveSheet oSheet.Cells(1, 2).Value = "testing" book.Save() book.Close() xls.Workbooks.Close() xls.Quit() ' NOTE: No calls the Marshal.ReleaseComObject() are ever needed End Sub
source share