Avatar of alainbryden
Flag 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()
            RestoreSettings() 'Restores menus and formula bar, etc.'
            xlApp.DisplayAlerts = False
            CloseFile() 'Closes the xlBook that this xlApp has open'
            xlApp = Nothing 'Error occurs here - yet this line is supposedly necessary to release all references to the object'
            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

.NET ProgrammingSystem ProgrammingVisual Basic.NET

Avatar of undefined
Last Comment
Christopher Kile

8/22/2022 - Mon
Christopher Kile

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.

If I take xlApp = Nothing out, the code executes but the Excel application does not terminate because there is still a reference to it.

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Christopher Kile

Interesting, and useful.  Go apply for a PAQ/refund, you've earned it :)
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck