问题
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:
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
来源:https://stackoverflow.com/questions/48909968/running-a-macro-before-printing-a-word-document