在 Word VBA 中捕获打印事件并非易事。然而,这是一个巧妙的技巧:)
为此,请执行以下操作
创建一个类模块说Class1
并粘贴此代码
Option Explicit
Public WithEvents oApp As Word.Application
Private Sub oApp_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
ActiveDocument.Bookmarks("DocWasPrinted").Delete
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="DocWasPrinted"
.DefaultSorting = wdSortByName
.ShowHidden = True
End With
End Sub
现在插入一个模块并粘贴此代码
Option Explicit
Dim oAppClass As New Class1
Public Sub AutoExec()
Set oAppClass.oApp = Word.Application
End Sub
Sub Testing()
If hasPrinted = True Then
MsgBox "Document was printed"
'~~> Call your code
Else
MsgBox "Please Print Before Adding"
End If
End Sub
Function hasPrinted() As Boolean
If ActiveDocument.Bookmarks.Exists("DocWasPrinted") = True Then
hasPrinted = True
End If
End Function
关闭您的文档并重新打开它。现在测试一下。
逻辑:
这段代码所做的是用户打印文档的那一刻,代码创建了一个名为的隐藏书签DocWasPrinted
,在我的代码中,我检查是否创建了书签。
请记住删除文档退出上的书签。
Private Sub Document_Close()
ActiveDocument.Bookmarks("DoWasPrinted").Delete
End Sub