Welcome to FreeSoftwareServers Confluence Wiki


You need to do Three things for this to work:

Open VBA Editor via ALT+F11

To create a module or class, right click and go Insert >> Module/Class

This needs to be run in order to have the macro running in the background waiting for a "Print Event"

  • After creating all macro's, run via either "run" or alt+F8 and select "Register_Event_Handler" >> Run
  • Then close and re-open doc and print and it should display a box "Before Print"

Insert >> Module

Dim X As New EventClassModule
Sub Register_Event_Handler()
Set X.App = Word.Application
End Sub

For this one, Double Click "ThisDocument" and paste this in the box that opens.

Private Sub Document_Open()
End Sub

Insert >> Class Module

Public WithEvents App As Word.Application
Private Sub App_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
' Run code directly inside this Sub OR
MsgBox "Before Print"
' Call another Sub here, note, Sub and Module name can't match
Call Greetings
' See https://www.freesoftwareservers.com/wiki/compile-error-expected-variable-or-procedure-not-module-macros-microsoft-office-29982732.html
' If calling a Sub which handles printing then you may want to add cancel statement here if it prints out 1 extra document
' Eg this is where I call my SerialNumber Sub which prints documents with increment serial numbers, then it got back to this macro
' and printed the last document again.
' I added the following and it fixed the problem
' Cancel = True
End Sub

  • No labels