PDA

View Full Version : Runnig a macro before printing



doleva
02-22-2018, 02:07 AM
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

Paul_Hossler
02-22-2018, 01:40 PM
https://msdn.microsoft.com/en-us/vba/word-vba/articles/application-documentbeforeprint-event-word


1. In a class module called clsWord



Option Explicit

Public WithEvents appWord As Word.Application

Private Sub appWord_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
Dim ans As Long
ans = MsgBox("Do you REALLY want to print this?", vbYesNo)
Cancel = (ans = vbNo)
End Sub



2. In a standard module



Option Explicit

Public msWord As clsWord




3. In ThisDocument





Option Explicit

Private Sub Document_New()
Set msWord = New ClsWord
Set msWord.appWord = Word.Application

End Sub

Private Sub Document_Open()
Set msWord = New ClsWord
Set msWord.appWord = Word.Application
End Sub