Re: Word automation, etc.



Hi =?Utf-8?B?d2lsc29uZA==?=,

Interesting. Personally, I wouldn't try to manipulate an uninstantiated document
object. For one thing, invoking the error handler would slow things down; it
would be faster (and more correct) to test if (wdapp.Documents.Count >0) and
only close the document if that evaluates to true.

Keep in mind: If document.close throws an error, your code will jump straight to
the catch. That's why the Word instance isn't being ended. You could conceivably
do this (pseudocode), although it would still not be as efficient:

try
document.close
catch
finally
wdapp.Quit

> Off the current subject, I was having a bit of trouble closing out the word
> instance. I don't understand why it didn't work the way I had it and after a
> modification to the code it does work now. Basically, when the application
> starts it creates an instance of word. If I then quit the application before
> a document was loaded, the word instance wouldn't die. If a document is
> loaded first then the app is quit, the word instance goes away.
>
> Anyway, here is the previous code (that wouldn't kill the word instance):
>
> public void CloseControl()
> {
> object dummy = System.Reflection.Missing.Value;
> object dummy2 = (object)false;
>
> try
> {
> document.Close( ref dummy2, ref dummy, ref dummy );
> wd.Quit( ref dummy2, ref dummy, ref dummy );
> }
> catch {}
> }
>
> This would give the "The Object invoked has disconnected from the clients"
> error when I would quit the app without loading a document.
>
> When I changed the code such that the .Close and .Quit methods were in
> separate try/catch blocks, the instance of word is killed and I don't get the
> error.
>
> public void CloseControl()
> {
> object dummy = System.Reflection.Missing.Value;
> object dummy2 = (object)false;
>
> try
> {
> document.Close( ref dummy2, ref dummy, ref dummy );
> }
> catch {}
>
> try
> {
> wd.Quit( ref dummy2, ref dummy, ref dummy );
> }
> catch{}
> }
>
> Any ideas why this happens?
>

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :-)

.


Loading