Proper disposal of Word 2007 (12.0) ApplicationClass in non-UI application?



I'm trying to find the proper way to dispose of my application instance. I
have a WordManager class that contains an Application as a member variable
(m_oApp). I made my class IDisposable and inside the destructor I call a its
dispose function which attempts to execute the following code:

object missingValue = Type.Missing;
object objDoNotSave = WdSaveOptions.wdDoNotSaveChanges;
if (this.m_oApp != null)
{
this.m_oApp.Quit(ref objDoNotSave, ref missingValue, ref missingValue);
this.m_oApp = null;
}

This application runs in the background without a UI so I can't just use the
Windows.Forms dispose events like I normally would.

This quit function works fine during normal runtime, but when I invoke it
from the destructor of my WordManager it gives me the following exception
and it never kills the winword.exe process.
InvalidObjectException
COM object that has been separated from its underlying RCW cannot be used.

I've considered several work-around options.

One, I can simply find a process that matches the name and timestamp of when
I instantiated ApplicationClass. (I don't want to do that because I might
actually have more than one legitimate instance of Word running at a time).
Two, I've searched the object model to see if I can find a handle or a
process id that I can just manually kill the winword.exe that belongs to
this instance.
Three, I can properly dispose the com instance and let it shut itself down.
(my preference).


What is the proper way to dispose of my COM word object when shutting down
the application?

Thanks in advance,
Nathan Bridgewater


.



Relevant Pages

  • Re: Fag Ends
    ... detritus into and then carry home to dispose of properly? ... litterers. ... The detrius was disposed of in proper ashtrays. ... Dropping fag ends on the floor and throwing them out of car windows has been the MO of many smokers for years. ...
    (uk.legal)
  • Re: Fag Ends
    ... detritus into and then carry home to dispose of properly? ... I despise ... The detrius was disposed of in proper ashtrays. ...
    (uk.legal)
  • Re: iraq murder video gi head cut off
    ... That's not the proper way to dispose of an American flag, Michele, and ...
    (microsoft.public.access.forms)
  • Re: Exception in Constructor
    ... RAII idiom avoids this. ... When you say proper us of RAII, do you mean that the code calling the ... guaranteed to call Dispose even if the constructor threw an exception? ... It's guaranteed to call Dispose whether or not the constructor throws ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Proper disposal of Word 2007 (12.0) ApplicationClass in non-UI application?
    ... this.m_oApp.Quit(ref objDoNotSave, ref missingValue, ref missingValue); ... the Windows.Forms dispose events like I normally would. ... This quit function works fine during normal runtime, ... I can properly dispose the com instance and let it shut itself ...
    (microsoft.public.office.developer.automation)