Understanding Document/Application events (Word 2003)

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15641
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Understanding Document/Application events (Word 2003)

Post by ChrisGreaves »

I would appreciate help in trapping one particular even in Word 2003, namely the large "Close" button, top-right corner, when I have TWO empty un-saved documents on the desktop).

The attached ZIP file holds the application template "Triggers.dot" and exported copies of its modules.
My various trigger-macros trap every instance of an end-user's action except the following:
  • I load MSWord with Triggers.dot in the Global set ("Startup" folder for MS Word).
  • Word presents me with an empty document "Document 1".
  • I start a new document "Document 2".
  • I click, with the mouse, on the large "close" button, and nothing from my Triggers.dot is activated.
  • Word closes the active document with nary a whisper.
Had I typed text into each document, Word would present me with the pop-up "Do you want to save the changes to Document 2?", but again, no diversion through my trap code.

Notes:
  1. I put a MsgBox with documents count at the head of every macro in case some weird error was terminating the macro without telling me; I figured at least I'd see something before the long silence.
  2. Putting Triggers.dot in the StartUp folder means that your FileClose commands are effectively disabled; i.e. "Don't try this at home, kids"
3.png
A few years ago I played around with the Document events, and this time I *think* I don't want document events in each document - they aren't my documents and I have no control over their content or code.It seems to me that really the only document event that should be of interest to me is a document event within my Triggers.dot, that detect when the application is being Quit, and hence should gracefully monitor the impact of a series of File-Closures on each open document.
You do not have the required permissions to view the files attached to this post.
Last edited by ChrisGreaves on 16 Apr 2021, 14:50, edited 1 time in total.
He who plants a seed, plants life.

User avatar
HansV
Administrator
Posts: 78568
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Understanding Document/Application events (Word 2003)

Post by HansV »

To initialize app, use an AutoExec macro in modTriggers:

Code: Select all

Public X As EventClassModule

Sub AutoExec()
    Set X = New EventClassModule
    Set X.App = Application
End Sub
You can't refer to ActiveDocument in app_Quit, for by the time this event is executed, all documents have been closed, so

Code: Select all

    MsgBox "appWord_Quit Enter - " & ActiveDocument.Name
would cause an error message that you can't debug. You can do this, though:

Code: Select all

    MsgBox "appWord_Quit Enter"
Best wishes,
Hans

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15641
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Understanding Document/Application events (Word 2003)

Post by ChrisGreaves »

HansV wrote:To initialize app, ... You can't refer to ActiveDocument in app_Quit, ... You can do this, though:
(later: Please see the end-result here)
Hans.
  • You da man!
  • You da man!
  • You da man!
I have attached updated versions of code for those who are interested (or who will follow)
I have to sit and think about the code and see how I'll use it; I appreciate that some of my "macro" code will be executed only after "it is too late", but here is where I stand ("on the shoulders of giants"):

I fire up word with Triggers.dot Global and two blank/empty/unchanged documents as before.

With "Document 2" active I click the Close button (I learned that the smaller button below it is called the Close Window button) Every one of these buttons has a name can display it when you position the mouse on top of the button.

This fires up the "App_DocumentBeforeClose" event, which means, I believe, I can deal with the document knowing that the user has instigated a Close.

Now I have only "Document 1" available, and it is, of course, Active.

With "Document 1" active I click the Close button
This fires up the "App_DocumentBeforeClose" event, and from this I infer that whenever the user clicks on the Close button I can have a single piece of code that deals with the event.

That's good to know.
But wait! There's more!
Because there was but one document, and the user has elected the Close, MS Word assumes that we are to close the application.
So next the "App_Quit" macro fires up. In theory I have no document processing to do at this point, but presumably I could log some status to a file.
Finally the "AutoExit" fires up.


So here's my take on the Close button in Word 2003:
When two or more documents are open, the Close button behaves like a File, Close (or Document Close) event.
When only one document is open, the Close button is meant to shut down the application.

Which leaves me wondering if anyone has ever programmed the Close button to effect a FileCloseAll whenever it is clicked, removing the ambiguity of the button.
You do not have the required permissions to view the files attached to this post.
He who plants a seed, plants life.