troubleshooting Question

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

Avatar of alainbryden
alainbrydenFlag for Canada asked on
.NET ProgrammingSystem ProgrammingVisual Basic.NET
4 Comments1 Solution2043 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
alainbryden

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros