在打印word文档之前运行宏

时间:2018-02-21 15:45:17

标签: word-vba

我在单词2010中有一个宏,它会在您打印文档时为文档添加页脚(即在打印预览屏幕中单击" final"打印按钮后)。

目前,为了填充文档,用户需要首先运行宏,并且只有在运行它之后,当他们打印文档时,才会添加页脚。

我想自动运行宏的部分,因此选择打印选项(Ctrl + P /文件>打印)将自动运行宏并打开打印预览屏幕以进行最终打印。

如何做到这一点?

提前谢谢

1 个答案:

答案 0 :(得分:1)

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

你需要做三件事才能实现这个目标:

通过ALT + F11打开VBA编辑器

要创建模块或类,请右键单击并插入>>模块/类

测试:关闭并重新打开并打印

  • 应显示一个显示“Before Print”的框

插入>>模块

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

enter image description here

相关问题