Error when Quitting Excel via VB.NET Automation

P

Peter Carr

Hi
I am creating an application which should open a new instance of Excel and
then Create, Populate and Save a new workbook before exiting. See code below.

I have references to Microsoft Excel 11.0 Object Library
(Microsoft.Office.Interop.Excel) and Microsoft Office 11.0 Object Library
(Microsoft.Office.Core).
All works as intended until I try and Quit the instance of Excel.
I am using VS2005 (CLR 2)

When I destroy the object based on CExcel I get the exception:

COM object that has been separated from its underlying RCW cannot be used.
The detail of the exception is:

System.Runtime.InteropServices.InvalidComObjectException was unhandled
Message="COM object that has been separated from its underlying RCW cannot
be used."
Source="Microsoft.Office.Interop.Excel"
StackTrace:
at Microsoft.Office.Interop.Excel.ApplicationClass.Quit()
at FRG.CExcel.Finalize()

any suggestions?

thanks

Peter
Here is the relevant code from the container class for Excel called CExcel:-

Imports Microsoft.Office.Interop
Public Class CExcel

Private mobjExcelApp As Excel.Application

'Create new Excel Instance
Public Sub New(ByVal vblnVisible As Boolean, ByVal vstrTemplateFolder As
String)
mobjExcelApp = New Excel.Application
mobjExcelApp.Visible = vblnVisible
End Sub

'Various methods for Creating, Populating, Saving and Closing Excel
Workbooks - ALL work fine

Protected Overrides Sub Finalize()
MyBase.Finalize()
If Not mobjExcelApp Is Nothing Then
mobjExcelApp.Quit() 'error happens here
End If
End Sub

End Class
 
C

Cindy M.

Hi =?Utf-8?B?UGV0ZXIgQ2Fycg==?=,
I am creating an application which should open a new instance of Excel and
then Create, Populate and Save a new workbook before exiting. See code below.

I have references to Microsoft Excel 11.0 Object Library
(Microsoft.Office.Interop.Excel) and Microsoft Office 11.0 Object Library
(Microsoft.Office.Core).
All works as intended until I try and Quit the instance of Excel.
I am using VS2005 (CLR 2)

When I destroy the object based on CExcel I get the exception:

COM object that has been separated from its underlying RCW cannot be used.
This sounds like a problem with GC. See if the information in this book excerpt
(and I highly recommend the book!) helps

http://msdn2.microsoft.com/en-us/library/aa679807(office.11).aspx

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top