我有一些代码想要在用户实际打印报告时触发。不是在打印预览等时,而是仅在发送到打印机时。用户需要能够提取报告并查看它,然后如果他们决定打印,VBA 代码将接管并将一些信息写入与用于生成报告的表不同的表中。我希望不必在实际报告上放置“打印”按钮(尽管我知道我可以隐藏它以进行打印),所以我想知道是否可以以某种方式捕获“打印”对话框。
有人曾经有过这样的运气吗?
经过深思熟虑,我认为实现此目的的最佳方法是在报告页面事件期间识别活动窗口文本。在打印预览期间,此文本将是数据库本身的名称,例如“Microsoft Access - DatabaseName : Database (Access 2003)”。在实际打印操作期间,活动窗口将是“打印”
我认为大部分代码来自这个来源 http://msdn.microsoft.com/en-us/library/aa141354%28v=office.10%29.aspx.
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)