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

N

Nathan

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
 
T

TD

I may be totally offbase here but I wonder if Quit() is decrementing the
reference count? I have also had a problem with Winword.exe staying open. It
seems to only do this if I make a call to Word::Selection::Cut() or
Word::Selection::Copy(). I thought it might be data in the clipboard keeping
it open but I tried clearing the clipboard before closing it down to no
avail. This doesn't happen with Office 97-2003 so I'm not real sure what's
going on there.
 
N

Nathan

Yeah that's odd. I did get my Quit function to work fine during regular run
time. It only throws this exception when it calls it from the destructor of
my class. Almost like com object is disconnected and destroyed before I even
hit my destructor. :)

My goal here is to make a conversion utility that a client assembly can
blindly call without having to worry about managing the Word instance. I set
it up using the provider model so I could potentially have other conversion
libraries that perform the same functions as Word if we decide to later
replace it. So really I want my custom implementation of the Word provider
to kill off the Word process when the whole app shuts down. I've got
everything setup except for disposing the word application.

ideas?

Thanks,
-Nathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top