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
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