Running a macro before printing a word document

╄→гoц情女王★ 提交于 2020-01-03 13:04:09

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!