Intercepting events like Save and Print

Article contributed by Dave Rado, Anna-Karin Bohman and Jonathan West

Intercepting commands

To intercept any Word command, you can:

1.

Press Alt+ F8 to bring up the Macros dialog and where it says Macros in,
select Word Commands”.

2.

Find and select one of the commands you want to intercept – for instance, to intercept the Print commands you need to find FilePrint and FilePrintDefault.  To intercept the Save commands you need to find FileSave, FileSaveAs and FileSaveAll

3.

Where it says Macros in, select the template you want to store the macro in, and click Create.

4.

The code needed to execute the command will be written for you;  just add your own code.

In the case of the Save event, writing FileSave, FileSaveAs and FileSaveAll macros isn't enough, because they won't intercept the user closing an unsaved document and being asked if they want to save changes – but you can intercept that by writing a macro called AutoClose;  or by writing a Document_Close event procedure in the ThisDocument code module.
  

See also:

Word commands, and their descriptions, default shortcuts and menu assignments

How to make the Paste Special dialog default to pasting Inline rather than Floating
  

If you are using a version of Word other than English
If you are not using an English version of Word, and if you create a macro using the name shown in the list of Word commands, only the description of what the macro does will be added to the new macro, not the necessary code. To get the necessary code, you have to create a macro using the English name for the command! But how do you find out the English name? You can get a full list of the English commands from here.

When you get the command's name right, the listbox at the very bottom will display the description of what the command does:

  

Intercepting events (Word 2000 or later)

Intercepting a command isn't quite the same as intercepting events, but in most cases it's the best you can do. However, in Word 2000 or later, a number of new Application Events were made available in VBA.

Two Application Events you can use include DocumentBeforeSave and DocumentBeforePrint. Both of these, but especially the former, work better than trying to intercept the relevant commands.

If not familiar with writing application event procedures, see the article: Writing application event procedures.

A DocumentBeforePrint event procedure looks like this:

Private Sub oApp_DocumentBeforePrint(ByVal Doc As Document, _
 Cancel As Boolean)
    'Your code here
End Sub

If you want to prevent printing from occurring in certain circumstances, you can set the Cancel variable to True, e.g.:

Private Sub oApp_DocumentBeforePrint(ByVal Doc As Document, _
 Cancel As Boolean)
    Dim Result As Long
    Result = MsgBox("Have you checked the " & "printer for letterhead paper?", vbYesNo)
    If Result = vbNo Then Cancel = True
End Sub

A DocumentBeforeSave procedure looks like this:

Private Sub oApp_DocumentBeforeSave(ByVal Doc As Document, _
  SaveAsUI As Boolean, Cancel As Boolean)
    'Your code here
End Sub

Again, you can set Cancel = True if you want to cancel the save.

If you set the SaveAsUI variable to True, the Save As dialog box will be displayed.