当用户实际打印报告时,我想要触发一些代码。不是在打印预览等时,而是仅在发送到打印机时。用户需要能够提取报告并查看报告,然后如果他们决定打印,则vba代码将接管并将一些信息写入与用于生成报告的表不同的表。我希望不必在实际报告上放置一个打印按钮(即使我知道我可以隐藏它用于打印),所以我想知道我是否能以某种方式陷阱打印对话框。
有没有人有运气呢?
答案 0 :(得分:2)
经过深思熟虑后,我认为实现此目的的最佳方法是在报告页面事件期间识别活动窗口文本。在打印预览期间,此文本将是数据库本身的名称,如“Microsoft Access - DatabaseName:Database(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