Home  |   French  |   About  |   Search  | mvps.org  

What's New
Table Of Contents
Credits
Netiquette
10 Commandments 
Bugs
Tables
Queries
Forms
Reports
Modules
APIs
Strings
Date/Time
General
Downloads
Resources
Search
Feedback
mvps.org

In Memoriam

Terms of Use


VB Petition

API: Suppress the "Printing" Dialog

Author(s)
Dev Ashish

(Q) How can I hide the "Printing" dialog that comes up when a report is sent of to a printer?

(A) The Printing dialog is an internal Access dialog, and as such, cannot be hidden directly from code.

    However, using a form's Timer event, we can minimize the dialog window moments after it comes up. This way, the window cannot be restored and it automatically destroys itself once the print job is completed.

    Note that the dialog might be visible for the amount of time specified in the form's TimerInterval property, and the time it takes Access to run this code.

The use of Timer event is crucial here since this is the only normal means of running any code when a modal window is displayed in Access.

Create a form (that you can open in Hidden mode) and set it's Timer property to 300. The OnTimer event will read as

Private Sub Form_Timer() 
    Call sWatchAccess(Application.hWndAccessApp)
End Sub

Now, from the form which has the Print Report button, put code similar to this

Private Sub Command0_Click()    
  On Error Resume Next    
  DoCmd.OpenForm "Form2"
  DoEvents:   DoEvents:   DoEvents
  DoCmd.OpenReport "Sales by Category", acViewNormal
  DoCmd.Close acForm, "Form2"
End Sub

where Form2 is the hidden form with the Timer event.

'************* Code Start *************
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
Private Declare Function apiGetClassName Lib "user32" _
  Alias "GetClassNameA" _
  (ByVal hWnd As Long, _
  ByVal lpClassname As String, _
  ByVal nMaxCount As Long) _
  As Long

Private Declare Function apiGetWindowText Lib "user32" _
  Alias "GetWindowTextA" _
  (ByVal hWnd As Long, _
  ByVal lpString As String, _
  ByVal aint As Long) _
  As Long
  
Private Declare Function apiGetLastActivePopup Lib "user32" _
  Alias "GetLastActivePopup" _
  (ByVal hWndOwnder As Long) _
  As Long
  
Private Declare Function apiShowWindow Lib "user32" _
  Alias "ShowWindow" _
  (ByVal hWnd As Long, _
  ByVal nCmdShow As Long) _
  As Long
  
Private Const MAX_LEN = 255
Private Const GW_HWNDNEXT = 2
Private Const SW_HIDE = 0
Private Const SW_MINIMIZE = 6
Private Const SW_SHOWMINNOACTIVE = 7
Private Const SW_SHOWDEFAULT = 10

Sub sWatchAccess(ByVal hWndApp As Long)
'Required: hWndAccessApp (Application handle)
'
  On Error GoTo Err_Handler
  Dim lnghWndChild As Long
  Dim strCaption As String
  Dim strClass As String
  Dim lngRet As Long
  
  'Get the last active popup in hWndApp instance
  lnghWndChild = apiGetLastActivePopup(hWndApp)
  strClass = fGetClassName(lnghWndChild)
  strCaption = fGetCaption(lnghWndChild)
  'is this the modal window?
  If strClass = "#32770" And Trim(strCaption) = "Printing" Then
    lngRet = apiShowWindow(lnghWndChild, SW_SHOWMINNOACTIVE)
  End If

Exit_Here:
  Exit Sub
Err_Handler:
  MsgBox "Error #: " & Err.Number & vbCrLf & Err.Description, _
    vbCritical + vbOKOnly, "sWatchAccess-Runtime Error"
  Resume Exit_Here
End Sub

Private Function fGetClassName(ByVal hWnd As Long) As String
Dim strBuffer As String
Dim lngRet As Long
  strBuffer = String$(32, 0)
  lngRet = apiGetClassName(hWnd, strBuffer, Len(strBuffer))
  If lngRet > 0 Then
    fGetClassName = Left$(strBuffer, lngRet)
  End If
End Function

Private Function fGetCaption(ByVal hWnd As Long) As String
Dim strBuffer As String
Dim lngRet As Long
  strBuffer = String$(MAX_LEN, 0)
  lngRet = apiGetWindowText(hWnd, strBuffer, Len(strBuffer))
  If lngRet > 0 Then
    fGetCaption = Left$(strBuffer, lngRet)
  End If
End Function
'************ Code End ***************

© 1998-2010, Dev Ashish & Arvin Meyer, All rights reserved. Optimized for Microsoft Internet Explorer