Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Intercepting Word's Exit function

361 views
Skip to first unread message

Darren McGuire

unread,
Mar 19, 2003, 11:24:48 PM3/19/03
to
Hi

Do you know what the command in Word is when you click on
the X in the top right corner? Also when you right click
on the Open document on the Task Bar and select close?

I need to know what these are as I need to intercept them
if a save is going to take place. I have a macro that
needs to run before a save which has been attached to the
following functions to date:

FileSave() FileSaveAs() Sub FileSaveAll()
FileSaveAsWebPage() FileSaveFrameAs() FileSaveHtml()
FileSaveVersion() FileCloseAll() FileClose()
FileExit() DocClose()

Functions such as the FileClose, FileExit, etc, only work
when you select them from the File Menu. DocClose is the
X in the top right corner for the current doc.

Thank You
Darren

Peter Hewett

unread,
Mar 19, 2003, 11:52:37 PM3/19/03
to
Darren

Word does not use a command per se when using the X (Close) icon. Word just
executes Application.Quit. Unfortunately even if you've modified the current
document Word still does not fire the FileSave or FileSaveAs macros!

What you'll need to do is work with the Word Application object can
instantiate you own event handler for Words Quit method. You may also need to
trap the DocumentBeforeClose event as well. It's not quite as bad as it
sounds!

Check out the VBA online help for "Using Events with the Application Object".

Post again if you need further help.

I hope this helps + Cheers - Peter


"Darren McGuire" <dazm...@hotmail.com> wrote in news:1a3301c2ee98$a4ec6270
$a401...@phx.gbl:

Peter Hewett

unread,
Mar 20, 2003, 12:29:47 AM3/20/03
to
Darren

I've done some further testing. It seems that you can catch most events by
registering an event handler in a template (as opposed to an AddIn). But
unfortunately you can't trap the Quit event using a template, you have to
use an AddIn. It looks like Word unloads the template before the Quit event
is fired!

Cheers - Peter


Peter Hewett <Nos...@xtra.co.nz> wrote in
news:Xns9344AB7C...@207.46.248.16:

Darren McGuire

unread,
Mar 20, 2003, 3:01:38 AM3/20/03
to
But an Addin is a template anyway isnt it ?

I was going to put my template with all this code into the
startup directory of Office so that it loaded everytime
with Word.

So if I understand you, I need some code that will reside
in my template that I am running as an Addin?

It sort of makes sense, but I have no idea how to code

Can you help ?

Thanks
Darren

>.
>

Jonathan West

unread,
Mar 20, 2003, 4:38:23 AM3/20/03
to

"Darren McGuire" <dazm...@hotmail.com> wrote in message
news:1a3301c2ee98$a4ec6270$a401...@phx.gbl...

Hi Darren,

In addition to the answers offered by Peter Hewett, you will find these
articles useful.

Distributing macros to other users
http://www.mvps.org/word/FAQs/MacrosVBA/DistributeMacros.htm

Intercepting events like Save and Print
http://www.mvps.org/word/FAQs/MacrosVBA/InterceptSavePrint.htm

What do Templates and Add-ins store?
http://www.mvps.org/word/FAQs/Customization/WhatTemplatesStore.htm

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
Word FAQs at http://www.multilinker.com/wordfaq
Please post any follow-up in the newsgroup. I do not reply to Word questions
by email

Peter Hewett

unread,
Mar 20, 2003, 6:39:43 AM3/20/03
to
Darren

Bacckground first. An AddIn is a template but it's not used to create
documents like templates are. Consequently any boilerplate text and styles
in an AddIn are irrelevant. You can in fact put your AddIn in one of two
folders. If you put it in the Office Startup folder then it's available to
all of a PC's users. If you put it in the Users Startup folder
(Tools/Options/File Locations) you can select which users load it.

Another difference between templates and AddIns are the Auto macros used.
The AutoNew and AutoOpen macros are fired when opening a new document based
on a template or opening an existing file created from that template. When
a template is used as an AddIn these macros don't fire. However, when an
AddIn is loaded its AutoExec macro is fired. Templates ignore AutoExec.

This code come in two chunks, the first chunk goes in a module. Create a
new module (say: modPublic) and paste in the following code. It will not
work in the ThisDocument module:


Public wehEventSink As New WordEventHandler

Public Sub AutoExec()
' Register the Event Handler
Set wehEventSink.WordApp = Word.Application
End Sub


This code goes in a Class module. Create a new class, you MUST call this
class "WordEventHandler". Paste in the following code:

Public WithEvents WordApp As Word.Application

Private Sub WordApp_DocumentOpen(ByVal docOpened As Word.Document)
MsgBox "WordApp_DocumentOpen event handler"
End Sub ' WordApp_DocumentOpen

Private Sub WordApp_DocumentBeforeClose(ByVal _
docClosing As Word.Document, _
ByRef Cancel As Boolean)
MsgBox "WordApp_DocumentBeforeClose event handler"
End Sub

Private Sub WordApp_Quit()
MsgBox "WordApp_Quit"
End Sub

As you can see i've coded a shell procedure for the following events:

DocumentOpen
DocumentBeforeClose
Quit

You get the idea of how they work and you can add additional event handlers
as needed. Be careful when adding your own code, the documentation is not
always spot on! Replace the message box code with your own.

Put your template in whatever startup folder you choose and then watch the
message boxes popup as Word responds to different events.

I hope this helps + Cheers - Peter


"Darren McGuire" <dazm...@hotmail.com> wrote in news:1b0f01c2eeb6$ef111d50
$a601...@phx.gbl:

Jay Freedman

unread,
Mar 20, 2003, 9:28:31 AM3/20/03
to
Jonathan West wrote:
> "Darren McGuire" <dazm...@hotmail.com> wrote in message
> news:1a3301c2ee98$a4ec6270$a401...@phx.gbl...
>> Hi
>>
>> Do you know what the command in Word is when you click on
>> the X in the top right corner? Also when you right click
>> on the Open document on the Task Bar and select close?
>>
[snip]

>
> Hi Darren,
>
> In addition to the answers offered by Peter Hewett, you will find
> these articles useful.
>
> Distributing macros to other users
> http://www.mvps.org/word/FAQs/MacrosVBA/DistributeMacros.htm
>
> Intercepting events like Save and Print
> http://www.mvps.org/word/FAQs/MacrosVBA/InterceptSavePrint.htm
>
> What do Templates and Add-ins store?
> http://www.mvps.org/word/FAQs/Customization/WhatTemplatesStore.htm

And these as well (we are spread a bit thin...):

Running a macro automatically when Word starts or quits
http://www.mvps.org/word/FAQs/MacrosVBA/ApplicationEvents.htm

Writing application event procedures
http://www.mvps.org/word/FAQs/MacrosVBA/AppClassEvents.htm

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://www.mvps.org/word


0 new messages