We help IT Professionals succeed at work.

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

2,038 Views
Last Modified: 2013-11-26
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

Comment
Watch Question

Christopher KileSenior Software Analyst
CERTIFIED EXPERT

Commented:
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.

Author

Commented:
If I take xlApp = Nothing out, the code executes but the Excel application does not terminate because there is still a reference to it.
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Christopher KileSenior Software Analyst
CERTIFIED EXPERT

Commented:
Interesting, and useful.  Go apply for a PAQ/refund, you've earned it :)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.