我在 Word 2010 中有一个宏,可以在打印文档时(即在打印预览屏幕中单击“最终”打印按钮后)向文档添加页脚。
目前,为了给文档添加页脚,用户需要先运行宏,只有运行宏后,在打印文档时才会添加页脚。
我想自动化运行宏的部分,以便选择打印选项(Ctrl+P /文件>打印)将自动运行宏并打开最终打印的打印预览屏幕。
这怎么办?
提前谢谢您
http://forums.whirlpool.net.au/archive/2603917
您需要做三件事才能使其发挥作用:
通过 ALT+F11 打开 VBA 编辑器
要创建模块或类,请右键单击并转到“插入>>模块/类”
测试:关闭并重新打开并打印
插入 >> 模块
Reg_Event_Handler
Dim X As New EventClassModule
Sub Register_Event_Handler()
Set X.App = Word.Application
End Sub
对于此文档,双击“ThisDocument”并将其粘贴到打开的框中。
Private Sub Document_Open()
Register_Event_Handler
End Sub
插入>>类模块
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