ms-access - 我在哪里放置 Access vba 代码以仅在物理打印时触发?

标签 ms-access vba ms-access-2003

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

有没有人幸运地这样做过?

最佳答案

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

我认为大部分代码来自 this source .

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

关于ms-access - 我在哪里放置 Access vba 代码以仅在物理打印时触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11347139/

相关文章:

SQL:如何从 int 中获取剩下的 3 个数字

excel - 每个循环的 VBA Excel

SQL - UNION 查询中的求和

ms-access - 如何通过网络查看谁在使用我的 Access 数据库?

variables - 如何使用 VBA 遍历 MS Access 记录集并将变量分配给结果?

vba - 哪个SUB称这个SUB

vba - 微软 Access 2010 : Adding transaction management into a form

excel - 如何在VBA中使用As声明多个指定类型的变量?

vba - Do While Not IsEmpty(Range ("A1")) 条件评估失败

mysql - 如何在Access 2013中对2个文本框求和?