firing event when word application closes

G

Gabriel RIvera

I have been reading up on how to fire events on vb when
documents open/close and also when the word application
quits... i have also read the document
http://www.mvps.org/word/FAQs/MacrosVBA/PseudoBeforeClose.
htm

which talks about the bug in the DocumentBeforeClose
event. My main problem is that i need to somehow know
when the user closes (hits on the x) the word
application. I have some code checking to see if
documents are open if so, it closes them.

in variable declaration on top..
(dim withevents wapp as new word.application)

in some method.

If wapp.Documents.Count > 0 Then


wapp.Documents.Close
(Word.WdSaveOptions.wdDoNotSaveChanges,
Word.WdOriginalFormat.wdWordDocument, False)

End If
********
the problem is thus the following if the user closes the
word application then the wapp.documents.count>0 will
give me an error( the RPC server is unavailable) since
their is no wapp object in the first place. I need to
know when the user closes the wapp application so i can
start it up again or bypass some lines of code...

Again, the article found here
(http://www.mvps.org/word/FAQs/MacrosVBA/PseudoBeforeClose
..htm
) has a way around this problem but i have used to code
and set breakpoints and i never seem to hit them--> event
now being fired on method...

THanks, a lot.
ps there are a lot of mistakes in msn library (html bugs)
search for example the documentBeforeClose event :~>

gabe
 
M

martinique

You can use

if not (wapp is nothing) then
....

However your initial declaration 'as new' will screw that up: wapp will
never be nothing. Most VB coding standards and all experienced programmers
I've met will tell you NEVER use 'as new', precisely for this reason.

Use

Dim withevents wapp as Word.Application
Set wapp = new Word.Application


A simpler approach to your immediate task is to trap the error, and skip the
function if there are no documents to close.
 

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