3

我有一些代码要在用户实际打印报告时触发。不是在打印预览等时,而是仅在发送到打印机时。用户需要能够拉出报告并查看它,然后如果他们决定打印,vba 代码将接管并将一些信息写入与用于生成报告不同的表中。我希望不必在实际报告上放置一个打印按钮(即使我知道我可以隐藏它以进行打印),所以我想知道我是否可以以某种方式捕获打印对话框。

有没有人有幸这样做过?

4

1 回答 1

3

经过深思熟虑,我认为最好的方法是在报告页面事件期间识别活动窗口文本。在打印预览期间,此文本将是数据库本身的名称,例如“Microsoft Access - DatabaseName : Database (Access 2003)。在实际打印操作期间,活动窗口将为“打印”

我相信大部分代码来自这个来源

Declare Function GetActiveWindow Lib "user32" () As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
   (ByVal Hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Private Sub Report_Page()
On Error GoTo PrintError

    Dim strCaption As String
       Dim lngLen   As Long

       ' Create string filled with null characters.
       strCaption = String$(255, vbNullChar)
       ' Return length of string.
       lngLen = Len(strCaption)

       ' Call GetActiveWindow to return handle to active window,
       ' and pass handle to GetWindowText, along with string and its length.
       If (GetWindowText(GetActiveWindow, strCaption, lngLen) > 0) Then
          ' Return value that Windows has written to string.
          ActiveWindowCaption = strCaption
       End If

    If ActiveWindowCaption = "Printing" Then

        '
        ' Special activity goes here.
        '

    End If

    Exit Sub

PrintError:
    ' Just in case

End Sub
于 2012-07-05T19:14:43.123 回答