How to keep Word Application in memory like what Outlook does?

J

John H

My VB program needs to keep an instance of Word App in memory for a long
period of time and prevents it quitting like what Outlook does.

If you use MS Word for the default editor and when you launch Outlook, an
instance of Word is launched in background and Outlook keeps it in memory for
as long as Outlook runs. Even when you double-click a .DOC file, the instance
will be used but when you click "X" button on top-right of Word UI, the
instance remains in memory.

How can I do it?

However I tried myself to launch a Word application via Word Automation but
if I double-click a .DOC file and then click "X" button on top-right of Word
UI, the Word instance is gone.


Thanks for any suggestion or idea!

John
 
C

Cindy M -WordMVP-

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

1. Your application would have to stay open and running

2. You need to declare the object for the Word.Application at the GLOBAL level,
so that it doesn't go out of scope when the procedure in which you instantiate
it ends.

3. Be careful to not set this object to Nothing until you're exiting your
application. And then make sure you clean up all the other Word objects you may
have "laying around", Quit the application and then set the object to Nothing.
My VB program needs to keep an instance of Word App in memory for a long
period of time and prevents it quitting like what Outlook does.

If you use MS Word for the default editor and when you launch Outlook, an
instance of Word is launched in background and Outlook keeps it in memory for
as long as Outlook runs. Even when you double-click a .DOC file, the instance
will be used but when you click "X" button on top-right of Word UI, the
instance remains in memory.

How can I do it?

However I tried myself to launch a Word application via Word Automation but
if I double-click a .DOC file and then click "X" button on top-right of Word
UI, the Word instance is gone.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
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 :)
 
J

John H

Thanks Cindy for your suggestion.

I did try to hold the Word.Application object for the entire program session
(i.e. release it at the program termiation time) but the instance of
Word.Application is used to open a .DOC file and then you click "X" button on
the top-right of Word UI (i.e. Quit). The instance is gone regardless
wheather any other program is hold it or not. It seems to me that the "Quit"
command always quits Word application.

An interesting finding is that if the Word instance is launched by Outlook
and the instance is also used to open a .DOC file, this time the "Quit"
command will not quit Word application. I am not sure how Outlook launches an
Word instance but it clearly has a different behaviour.

John H
 
C

Cindy M -WordMVP-

Hi John,
I did try to hold the Word.Application object for the entire program session
(i.e. release it at the program termiation time) but the instance of
Word.Application is used to open a .DOC file and then you click "X" button on
the top-right of Word UI (i.e. Quit). The instance is gone regardless
wheather any other program is hold it or not. It seems to me that the "Quit"
command always quits Word application.
I suppose Outlook is able to cancel the Quit event and just make the application
window invisible, instead <sigh>

The closest I could find in the Knowledge Base is the following article.
Although it doesn't address the particular button you're concerned about, the
suggested approach might help you find the solution you need:

http://support.microsoft.com/kb/192733/en-us

A classic VB group can probably help you find the right API function.
An interesting finding is that if the Word instance is launched by Outlook
and the instance is also used to open a .DOC file, this time the "Quit"
command will not quit Word application. I am not sure how Outlook launches an
Word instance but it clearly has a different behaviour.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
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 :)
 
J

Jean-Guy Marcil

John H was telling us:
John H nous racontait que :
Thanks Cindy for your suggestion.

I did try to hold the Word.Application object for the entire program
session (i.e. release it at the program termiation time) but the
instance of Word.Application is used to open a .DOC file and then you
click "X" button on the top-right of Word UI (i.e. Quit). The
instance is gone regardless wheather any other program is hold it or
not. It seems to me that the "Quit" command always quits Word
application.

An interesting finding is that if the Word instance is launched by
Outlook and the instance is also used to open a .DOC file, this time
the "Quit" command will not quit Word application. I am not sure how
Outlook launches an Word instance but it clearly has a different
behaviour.

I tried the following code from Excel (after setting a reference to the Word
object library).
By the way, what Word version are you using?

I tested the following with Office 2003.

'_______________________________________
Public appWord As Word.Application
'_______________________________________
Sub Create_Word()

Set appWord = New Word.Application
With appWord
.Documents.Add
End With

End Sub
'_______________________________________

'_______________________________________
Sub Quit_word()

appWord.Visible = True
appWord.Quit wdDoNotSaveChanges

End Sub
'_______________________________________

I launched "Create_word".

I check in the Windows Task Manager (WTM) that WINWORD was present (it was)
because it was invisible in the GUI.

I clicked on the Word icon on my quick launch toolbar.
Document2 was on the screen (because Document1 was being used by the
invisible instance.)
I edited Document2 a bit, then clicked on the X on the title bar to close
Word.
After that I checked the WTM, my WINWORD instance was still there.
Finally, I debugged "Quit_Word", sure enough, Document1 appeared on the
screen, then the Word instance was closed.

I tried again, but this time, before running "Quit_word", I closed Excel.
My WINWORD instance was still in the WTM, but I had to use the WTM to end
the process because it was otherwise invisible to the GUI (and of course I
could not run "Quit_Word" anymore...).

Isn't this what you were writing about (and what Cindy recommended that you
do)?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

John H

Thanks to both Cindy and Jean-Guy.

The solution by adding an empty document will work for me as long as the
empty document is not being closed. I really appreciate the help from you
guys.

One thing I notice and which is different from the solution above is that
Outlook does not use the "Adding an empty document" approach because the
document count is still "Document1" or maybe Outlook knows how to hide the
empty document from Word UI.

However more interestingly I can "quit" the Word instance launched by
Outlook by using the following VB code:
Set gWordApp = GetObject(, "Word.Application")
gWordApp.Quit wdDoNotSaveChanges
Set gWordApp = Nothing

So the only difference between the Word instance launched by my VB and the
Word instance by Outlook is that they handle the "exit" command and the
top-right "X" button.

Thanks again for insights.

John H
 

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