5
votes

I have a macro in word 2010, that adds a footer to documents when you print them (i.e. after you click the "final" print button, in the print preview screen).

Currently, in order to foot the document, the user needs to run the macro first, and only after running it, when they print the document, the footer is added.

I would like to automate the part of running the macro, so that selecting a printing option (Ctrl+P / File>Print) would run the macro automatically and open the print preview screen for the final printing.

How can this be done?

Thank you in advance

1
I saw an article on how to do that very thing in Excel using the BeforePrint event. Perhaps that could be adapted to your needs? answers.microsoft.com/en-us/office/forum/office_2010-customize/…Stilgar
You can assign any macro to a keyboard shortcut. Whether the keyboard shortcut can continue in the user interface in the manner you wish, however, depends to a certain extent which version of Word is involved.Cindy Meister

1 Answers

1
votes

http://forums.whirlpool.net.au/archive/2603917

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

To Test: Close and Re-Open and Print

  • It should display a box saying "Before Print"

Insert >> Module

Reg_Event_Handler


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()
Register_Event_Handler
End Sub

Insert >> Class Module

EventClassModule


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
End Sub

enter image description here