Excel Not closing when COM objects are released
Excel Not closing when COM objects are released
Excel Not closing when COM objects are released
Excel Not closing when COM objects are released
Excel Not closing when COM objects are released
Excel Not closing when COM objects are released Excel Not closing when COM objects are released Excel Not closing when COM objects are released Excel Not closing when COM objects are released Excel Not closing when COM objects are released Excel Not closing when COM objects are released Excel Not closing when COM objects are released Excel Not closing when COM objects are released
Excel Not closing when COM objects are released Excel Not closing when COM objects are released
Excel Not closing when COM objects are released
Go Back  Xtreme Visual Basic Talk > > > Excel Not closing when COM objects are released


Reply
 
Thread Tools Display Modes
  #1  
Old 04-15-2008, 04:21 PM
A Gray A Gray is offline
Newcomer
 
Join Date: Apr 2008
Location: Maryland
Posts: 3
Default


I too am having this problem. Any help is greatly appreciated.
I am using VS 2005 w/ Excel 2003.

Note: When the lines relating to "WrkBook" are removed the "EXCEL" process does die when the routine finishes.

Here is the source code

Quote:
Sub Main()
Dim objExcel As Excel.Application = Nothing
Dim WrkBooks As Excel.Workbooks = Nothing
Dim WrkBook As Excel.Workbook = Nothing

objExcel = New Excel.Application

WrkBooks = objExcel.Workbooks
WrkBook = WrkBooks.Add()

GC.Collect()
GC.WaitForPendingFinalizers()

WrkBook.Close(SaveChanges:=False)
Dim n As Integer = -1
Do
n = System.Runtime.InteropServices.Marshal.FinalReleaseComObject(WrkBook)
Loop Until n = 0
WrkBook = Nothing

WrkBooks.Close()
Do
n = System.Runtime.InteropServices.Marshal.FinalReleaseComObject(WrkBooks)
Loop Until n = 0
WrkBooks = Nothing

objExcel.Quit()
Do
n = System.Runtime.InteropServices.Marshal.FinalReleaseComObject(objExcel)
Loop Until n = 0
objExcel = Nothing

GC.Collect()
GC.WaitForPendingFinalizers()
End Sub

Last edited by lebb; 04-16-2008 at 11:01 AM. Reason: Split to separate thread
Reply With Quote
  #2  
Old 04-16-2008, 10:24 AM
Mike Rosenblum's Avatar
Mike Rosenblum Mike Rosenblum is offline
Microsoft Excel MVP

Forum Leader
* Guru *
 
Join Date: Jul 2003
Location: New York, NY, USA
Posts: 7,848
Default

Hi A Gray,

Welcome to the forum.

I have no problem running your code as-is and my Excel instance does not hang. That said, a couple of comments:

(1) I recommend calling GC.Collect() and GC.WaitForPendingFinalizers() twice up front before explicitly releasing your COM objects. Your way of splitting it is probably fine most of the time, but you'll "improve your odds" by putting them both up front.

(2) You do not need to call Marshal.FinalReleaseComObject() repeatedly in a loop as was previously necessary with Marshal.ReleaseComObject(). In fact, this is why Marshal.FinalReleaseComObject() was created: it does the looping for you internally, decrementing the ref count of the COM object repeatedly until it reaches zero (0) and then returns control back to your code.

With this in mind, I would try the following:
Code:
    Sub Main()
        Dim objExcel As Excel.Application = Nothing
        Dim WrkBooks As Excel.Workbooks = Nothing
        Dim WrkBook As Excel.Workbook = Nothing

        objExcel = New Excel.Application

        WrkBooks = objExcel.Workbooks
        WrkBook = WrkBooks.Add()

        GC.Collect()
        GC.WaitForPendingFinalizers()
        GC.Collect()
        GC.WaitForPendingFinalizers()

        WrkBook.Close(SaveChanges:=False)
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(WrkBook)

        WrkBooks.Close()
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(WrkBooks)

        objExcel.Quit()
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(objExcel)
    End Sub
I don't know if it will run any better for you, but it should have a better chance. I have my fingers crossed!

Mike
__________________
My Articles:
| Excel from .NET | Excel RibbonX using VBA | Excel from VB6 | CVErr in .NET | MVP |
Avatar by Lebb

Last edited by Mike Rosenblum; 04-16-2008 at 02:22 PM.
Reply With Quote
  #3  
Old 04-16-2008, 12:41 PM
A Gray A Gray is offline
Newcomer
 
Join Date: Apr 2008
Location: Maryland
Posts: 3
Default

Mike,

Thank you!! That did the trick.
Reply With Quote
  #4  
Old 04-16-2008, 01:14 PM
Mike Rosenblum's Avatar
Mike Rosenblum Mike Rosenblum is offline
Microsoft Excel MVP

Forum Leader
* Guru *
 
Join Date: Jul 2003
Location: New York, NY, USA
Posts: 7,848
Default

Ah, another satisfied customer!

You might want to read more about this in our Automating Office Programs with VB.NET tutorial. It discusses a number of issues involved with trying to close down the Excel application properly and it explains why I think that this approach -- specifically, calling GC.Collect() and GC.WaitForPendingFinalizers() twice *first* instead of last -- is probably the best way to go. (I've yet to see it fail!)

Good luck A Gray, and come back any time...
__________________
My Articles:
| Excel from .NET | Excel RibbonX using VBA | Excel from VB6 | CVErr in .NET | MVP |
Avatar by Lebb

Last edited by Mike Rosenblum; 04-16-2008 at 02:21 PM.
Reply With Quote
  #5  
Old 04-17-2008, 09:21 AM
A Gray A Gray is offline
Newcomer
 
Join Date: Apr 2008
Location: Maryland
Posts: 3
Default

Thanks again for your assistance.

A new twist. The code sample worked without a hitch using Excel 2002. Today, I am trying the same code using Excel 2003 and it isn't working.

Any ideas?
Reply With Quote
  #6  
Old 04-17-2008, 12:01 PM
Mike Rosenblum's Avatar
Mike Rosenblum Mike Rosenblum is offline
Microsoft Excel MVP

Forum Leader
* Guru *
 
Join Date: Jul 2003
Location: New York, NY, USA
Posts: 7,848
Default

That same, exact code? It ran fine for me using Excel 2003.

Worst case you can use the System.Diagnostics.Process class to shut down the Excel.Application when you are done. You should be sure to get the process ID of your Excel.Application when you first open it, that way you can know for certain which Excel process to shut down when done. We discuss how to do this here: Closing your App -- Extreme Measures.

However, I would not do this unless really necessary. Usually the problem can be found and fixed. (I have never had to use Process.Kill yet, myself.) But if you have to, it's there and runs fine so long as your code has full trust.
__________________
My Articles:
| Excel from .NET | Excel RibbonX using VBA | Excel from VB6 | CVErr in .NET | MVP |
Avatar by Lebb
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Excel Not closing when COM objects are released
Excel Not closing when COM objects are released
Excel Not closing when COM objects are released Excel Not closing when COM objects are released
Excel Not closing when COM objects are released
Excel Not closing when COM objects are released
Excel Not closing when COM objects are released Excel Not closing when COM objects are released Excel Not closing when COM objects are released Excel Not closing when COM objects are released Excel Not closing when COM objects are released Excel Not closing when COM objects are released Excel Not closing when COM objects are released
Excel Not closing when COM objects are released
Excel Not closing when COM objects are released
 
Excel Not closing when COM objects are released
Excel Not closing when COM objects are released
 
-->