Board index » delphi » How do I close close my connection to word and still leave the document open.

How do I close close my connection to word and still leave the document open.

I would like to close my TwordApplication and leave word running and then
reuse the TwordApplication to start up another instance of word.

If I use wordApplication1.Dissconnect it appears to work if I leave the
first word window open (at each start I have a new instance of word start -
ckNewInstance).  But if I close the orginal word application and then try to
connect using wordApplication1 I get a RPC error.

I saw the post where you have to wordApplication.Quit before
wordApplication1.Disconnect  but this closes the instance of word.

Thanks,

DJ McNeill

 

Re:How do I close close my connection to word and still leave the document open.


<<DJ McNeill:
I saw the post where you have to wordApplication.Quit
before wordApplication1.Disconnect  but this closes the
instance of word.

Quote

Yes, you don't have to call Quit if you don't want Word to
quit. But you must make sure you disconnect everything,
document components before the application component, and
don't accidentally connect them afterwards by calling one
of their methods.

--
Deborah Pate (TeamB) http://delphi-jedi.org

  Use Borland servers; TeamB don't see posts via ISPs
  http://www.borland.com/newsgroups/genl_faqs.html

Re:How do I close close my connection to word and still leave the document open.


So if I want to repeatly open a document template into a word document I
will have to dynamically create a TWordApplication each time?

If this is the case how do I clean up.  I will be creating a bunch of
TWordApplication with no free's.

Currently I have dropped a TWordApplication and 2TWordDocuments.  I use one
document to hold the template and copy it to the 2 document (all are linked
to the same TWordApplication) and then fill in address and other information
at bookmarks.  It works great once.  If I try to run the function again it
gives error messages.  I think because I am trying to reuse the
TWordApplication and TWordDocuments.

So what is the correct method of allowing this function to run multiple
times.  Do I have to dynamically create a TWordApplication and 2
TWordDocuments each time I run this function.

Hope you can help out.

Thanks a bunch.

DJ McNeill

Re:How do I close close my connection to word and still leave the document open.


<<DJ McNeill:
So if I want to repeatly open a document template into a
word document I will have to dynamically create a
TWordApplication each time?

Quote

No, certainly not!

All these components have Connect and Disconnect methods.
These have nothing to do with creating and freeing the
components, but they control the components' connection to
active instances of Word and Word documents. The error you
described in your first post comes from having a Word
application component that thinks it is connected to a Word
application instance, when the latter has actually been
closed. (It doesn't matter if another one has been started
later, the component still refers to the closed one.) When
it tries to access this no-longer-existent application,
there is naturally an error. To stop this from happening
you have to call the Disconnect method of a component
whenever the object it is connected to is closed.

<<DJ McNeill:
So what is the correct method of allowing this function to
run multiple times.

Quote

At the beginning of the function, where you open the
document, use the TWordDocument's ConnectTo method to
connect it to the document. At the end of the function,
call the TWordDocument's Disconnect method. If Word itself
will be closed at any point, call the TWordDocument's and
TWordApplication's Disconnect methods.

--
Deborah Pate (TeamB) http://delphi-jedi.org

  Use Borland servers; TeamB don't see posts via ISPs
  http://www.borland.com/newsgroups/genl_faqs.html

Other Threads