Link to home
Start Free TrialLog in
Avatar of alainbryden
alainbrydenFlag for Canada

asked on

Unusual Exception thrown when setting Excel.Application COM object = Nothing (using VB.NET)

I'm getting a strange exception. I thought that setting variableName = Nothing was a simple reference assignment, and that no exception could possibly occur by essentially removing a reference to an object, but here we stand.

When running the code below, I get an exception on the line xlApp = Nothing.

The variable xlApp is a private WithEvents Excel.Application.




Strangely enough, quite a stack trace is generated from this single command. I would have though it was a simple variable assignment:

{"Exception has been thrown by the target of an invocation."}
Inner Exception: {"COM object that has been separated from its underlying RCW cannot be used."}
                            at Microsoft.Office.Interop.Excel.AppEvents_EventProvider..ctor(Object )

Outer Exception Stack Trace:
    at System.RuntimeMethodHandle._InvokeConstructor(Object[] args, SignatureStruct& signature, IntPtr declaringType)
    at System.RuntimeMethodHandle.InvokeConstructor(Object[] args, SignatureStruct signature, RuntimeTypeHandle declaringType)
    at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
    at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
    at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
    at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture)
    at System.__ComObject.CreateEventProvider(Type t)
    at System.__ComObject.GetEventProvider(Type t)
    at Microsoft.Office.Interop.Excel.ApplicationClass.remove_SheetChange(AppEvents_SheetChangeEventHandler )
    at QuartzShare.ExcelHoster.set_xlApp(Application WithEventsValue)
    at QuartzShare.ExcelHoster.CloseApp() in ExcelHoster.vb:line 252


Thank you for any help in determining the source of, or workaround for this error. It's difficult to research as the exceptions thrown are primarily generic. I wouldn't consider setting xlApp = nothing as 'Use' of a released com object either, so I don't know why that is being suggested by the inner exception.

--
Alain Bryden

Private WithEvents xlApp As Excel.Application 'The running excel application belonging to this control'
 
    'Can be called to terminate the excel application'
    Public Sub CloseApp()
        Try
            RestoreSettings() 'Restores menus and formula bar, etc.'
            xlApp.DisplayAlerts = False
            CloseFile() 'Closes the xlBook that this xlApp has open'
            xlApp.Quit()
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
            xlApp = Nothing 'Error occurs here - yet this line is supposedly necessary to release all references to the object'
            GC.Collect()
            GC.WaitForPendingFinalizers()
            ErrMessage = Nothing
        Catch ex As Exception
            MsgBox("Could not release the excel resources: " & ex.Message, , Application.ProductName)
        End Try
    End Sub

Open in new window

Avatar of Christopher Kile
Christopher Kile
Flag of United States of America image

Does it work if you take the xlApp = Nothing statement away?  My guess would be yes...but check for hanging instances of Excel after you close your app.
Avatar of alainbryden

ASKER

If I take xlApp = Nothing out, the code executes but the Excel application does not terminate because there is still a reference to it.
ASKER CERTIFIED SOLUTION
Avatar of alainbryden
alainbryden
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Interesting, and useful.  Go apply for a PAQ/refund, you've earned it :)