1

我只想在文档已经打印的情况下启用另一个功能“代码”,我在想一些类似的东西

Sub Testing
    Dim hasPrinted as boolean
    If ActiveDocument.PrintOut = True Then
        hasPrinted = True
        call code here...
    Else
        hasPrinted = False
        MsgBox "Please Print Before Adding"
    End If
End Sub

我在“ActiveDocument.PrintOut”行收到一条错误消息,显示“编译错误、预期的函数或变量”。谁能给我一些指示?

4

2 回答 2

2

在 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
于 2012-07-16T19:18:18.853 回答
1

此问题提供有关在打印后创建临时文档事件的信息。

完成此操作后,您可以将布尔值更新为 true 以指示文档已打印。Word 不会以本机方式存储此信息。

于 2012-07-16T19:09:44.997 回答