PRB: Working with Print Dialog and Printer Object under NT 4.0

Last reviewed: March 19, 1998
Article ID: Q173981
The information in this article applies to:
  • Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 5.0 on the following platform: NT
  • Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 16-bit and 32-bit, for Windows, version 4.0 on the following platform: NT

SYMPTOMS

With Microsoft Visual Basic versions 4.0 and 5.0 on Windows NT version 4.0, when you use the ShowPrinter method of the Common Dialog Control to allow the user to set properties of the Printer object, such as the Copies and Orientation properties, these properties are not affected by the selection in the Print dialog box.

RESOLUTION

To work around this problem, use the Windows API to display the Printer dialog and then set the Printer object's properties to those selected by the user. The following steps illustrate this solution:

  1. Start a new project with the default Form1.

  2. Add two Command buttons to Form1. Set the following properties for the controls:

                                    Name Property         Caption Property
                                    -------------         ----------------
       First Command button :       cmdPrint              Print Dialog
       Second Command button:       cmdPrintSetup         Print Setup Dialog
    
    

  3. Add a module to the project.

  4. Add the following code to the module:

          ' Global constants for Win32 API
          Public Const CCHDEVICENAME = 32
          Public Const CCHFORMNAME = 32
          Public Const GMEM_FIXED = &H0
          Public Const GMEM_MOVEABLE = &H2
          Public Const GMEM_ZEROINIT = &H40
          Public Const DM_DUPLEX = &H1000&
          Public Const DM_ORIENTATION = &H1&
          Public Const PD_ALLPAGES = &H0
          Public Const PD_COLLATE = &H10
          Public Const PD_DISABLEPRINTTOFILE = &H80000
          Public Const PD_ENABLEPRINTHOOK = &H1000
          Public Const PD_ENABLEPRINTTEMPLATE = &H4000
          Public Const PD_ENABLEPRINTTEMPLATEHANDLE = &H10000
          Public Const PD_ENABLESETUPHOOK = &H2000
          Public Const PD_ENABLESETUPTEMPLATE = &H8000
          Public Const PD_ENABLESETUPTEMPLATEHANDLE = &H20000
          Public Const PD_HIDEPRINTTOFILE = &H100000
          Public Const PD_NONETWORKBUTTON = &H200000
          Public Const PD_NOPAGENUMS = &H8
          Public Const PD_NOSELECTION = &H4
          Public Const PD_NOWARNING = &H80
          Public Const PD_PAGENUMS = &H2
          Public Const PD_PRINTSETUP = &H40
          Public Const PD_PRINTTOFILE = &H20
          Public Const PD_RETURNDC = &H100
          Public Const PD_RETURNDEFAULT = &H400
          Public Const PD_RETURNIC = &H200
          Public Const PD_SELECTION = &H1
          Public Const PD_SHOWHELP = &H800
          Public Const PD_USEDEVMODECOPIES = &H40000
          Public Const PD_USEDEVMODECOPIESANDCOLLATE = &H40000
    

          'Custom Global Constants
          Public Const DLG_PRINT = 0
          Public Const DLG_PRINTSETUP = 1
    

          'type definitions:
          Type PRINTDLG_TYPE
    
                  lStructSize As Long
                  hwndOwner As Long
                  hDevMode As Long
                  hDevNames As Long
                  hdc As Long
                  flags As Long
                  nFromPage As Integer
                  nToPage As Integer
                  nMinPage As Integer
                  nMaxPage As Integer
                  nCopies As Integer
                  hInstance As Long
                  lCustData As Long
                  lpfnPrintHook As Long
    
                  lpfnSetupHook As Long
                  lpPrintTemplateName As String
                  lpSetupTemplateName As String
                  hPrintTemplate As Long
                  hSetupTemplate As Long
          End Type
    
          Type DEVNAMES_TYPE
                  wDriverOffset As Integer
                  wDeviceOffset As Integer
                  wOutputOffset As Integer
                  wDefault As Integer
                  extra As String * 100
          End Type
    
          Type DEVMODE_TYPE
                  dmDeviceName As String * CCHDEVICENAME
                  dmSpecVersion As Integer
                  dmDriverVersion As Integer
                  dmSize As Integer
                  dmDriverExtra As Integer
                  dmFields As Long
                  dmOrientation As Integer
                  dmPaperSize As Integer
                  dmPaperLength As Integer
                  dmPaperWidth As Integer
                  dmScale As Integer
                  dmCopies As Integer
                  dmDefaultSource As Integer
                  dmPrintQuality As Integer
                  dmColor As Integer
                  dmDuplex As Integer
                  dmYResolution As Integer
                  dmTTOption As Integer
                  dmCollate As Integer
                  dmFormName As String * CCHFORMNAME
                  dmUnusedPadding As Integer
                  dmBitsPerPel As Integer
                  dmPelsWidth As Long
                  dmPelsHeight As Long
                  dmDisplayFlags As Long
                  dmDisplayFrequency As Long
          End Type
    
          'API declarations:
          Public Declare Function PrintDialog Lib "comdlg32.dll" _
             Alias "PrintDlgA" (pPrintdlg As PRINTDLG_TYPE) As Long
    
          Public Declare Sub CopyMemory Lib "kernel32" _
             Alias "RtlMoveMemory" _
             (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
    
          Public Declare Function GlobalLock Lib "kernel32" _
             (ByVal hMem As Long) As Long
    
          Public Declare Function GlobalUnlock Lib "kernel32" _
             (ByVal hMem As Long) As Long
    
          Public Declare Function GlobalAlloc Lib "kernel32" _
             (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
    
          Public Declare Function GlobalFree Lib "kernel32" _
             (ByVal hMem As Long) As Long
    
          Public Sub ShowPrinter(frmOwner as Form, _
              Optional PrintFlags As Integer)
    
              Dim PrintDlg As PRINTDLG_TYPE
              Dim DevMode As DEVMODE_TYPE
              Dim DevName As DEVNAMES_TYPE
    
              Dim lpDevMode As Long, lpDevName As Long
              Dim bReturn As Integer
              Dim objPrinter As Printer, NewPrinterName As String
              Dim strSetting as String
    
              ' Use PrintDialog to get the handle to a memory
              ' block with a DevMode and DevName structures
    
              PrintDlg.lStructSize = Len(PrintDlg)
              PrintDlg.hwndOwner = frmOwner.hWnd
    
              PrintDlg.flags = PrintFlags
    
              'Set the current orientation and duplex setting
              DevMode.dmDeviceName = Printer.DeviceName
              DevMode.dmSize = Len(DevMode)
              DevMode.dmFields = DM_ORIENTATION Or DM_DUPLEX
              DevMode.dmOrientation = Printer.Orientation
              On Error Resume Next
              DevMode.dmDuplex = Printer.Duplex
              On Error GoTo 0
    
              'Allocate memory for the initialization hDevMode structure
              'and copy the settings gathered above into this memory
              PrintDlg.hDevMode = GlobalAlloc(GMEM_MOVEABLE Or _
                 GMEM_ZEROINIT, Len(DevMode))
              lpDevMode = GlobalLock(PrintDlg.hDevMode)
              If lpDevMode > 0 Then
                  CopyMemory ByVal lpDevMode, DevMode, Len(DevMode)
                  bReturn = GlobalUnlock(PrintDlg.hDevMode)
              End If
    
              'Set the current driver, device, and port name strings
              With DevName
                  .wDriverOffset = 8
                  .wDeviceOffset = .wDriverOffset + 1 + Len(Printer.DriverName)
                  .wOutputOffset = .wDeviceOffset + 1 + Len(Printer.Port)
                  .wDefault = 0
              End With
              With Printer
                  DevName.extra = .DriverName & Chr(0) & _
                  .DeviceName & Chr(0) & .Port & Chr(0)
              End With
    
              'Allocate memory for the initial hDevName structure
              'and copy the settings gathered above into this memory
              PrintDlg.hDevNames = GlobalAlloc(GMEM_MOVEABLE Or _
                  GMEM_ZEROINIT, Len(DevName))
              lpDevName = GlobalLock(PrintDlg.hDevNames)
              If lpDevName > 0 Then
                  CopyMemory ByVal lpDevName, DevName, Len(DevName)
                  bReturn = GlobalUnlock(lpDevName)
              End If
    
              'Call the print dialog up and let the user make changes
              If PrintDialog(PrintDlg) Then
    
                  'First get the DevName structure.
                  lpDevName = GlobalLock(PrintDlg.hDevNames)
                      CopyMemory DevName, ByVal lpDevName, 45
                  bReturn = GlobalUnlock(lpDevName)
                  GlobalFree PrintDlg.hDevNames
    
                  'Next get the DevMode structure and set the printer
                  'properties appropriately
                  lpDevMode = GlobalLock(PrintDlg.hDevMode)
                      CopyMemory DevMode, ByVal lpDevMode, Len(DevMode)
                  bReturn = GlobalUnlock(PrintDlg.hDevMode)
                  GlobalFree PrintDlg.hDevMode
                  NewPrinterName = UCase$(Left(DevMode.dmDeviceName, _
                      InStr(DevMode.dmDeviceName, Chr$(0)) - 1))
                  If Printer.DeviceName <> NewPrinterName Then
                      For Each objPrinter In Printers
                         If UCase$(objPrinter.DeviceName) = NewPrinterName Then
                              Set Printer = objPrinter
                         End If
                      Next
                  End If
                  On Error Resume Next
    
                  'Set printer object properties according to selections made
                  'by user
                  With Printer
                      .Copies = DevMode.dmCopies
                      .Duplex = DevMode.dmDuplex
                      .Orientation = DevMode.dmOrientation
                  End With
                  On Error GoTo 0
              End If
    
              'Display the results in the immediate (debug) window
              With Printer
                  If .Orientation = 1 Then
                      strSetting = "Portrait. "
                  Else
                      strSetting = "Landscape. "
                  End If
                  Debug.Print "Copies = " & .Copies, "Orientation = " & _
                     strSetting
              End With
    
          End Sub
    
    

  5. Add the following code to Form1:

          Private Sub cmdPrint_Click()
    
             ShowPrinter Me
          End Sub
    
          Private Sub cmdPrintSetup_Click()
             ShowPrinter Me, PD_PRINTSETUP
          End Sub
    
    

  6. Press the F5 key to run the application.

  7. Click the Command buttons. Note that the correct settings for Copies and Orientation are displayed in the Immediate Window (or Debug Window).

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Start a new project on an NT 4.0 machine with default Form1.

  2. Add a CommandButton and a Common Dialog control to Form1.

  3. Add the following code to the Click event of Command1:

          CommonDialog1.ShowPrinter
          Debug.Print "Copies = " & Printer.Copies; " & Orientation = " & _
    
              Printer.Orientation
    
    

  4. Press the F5 key to run the application.

  5. Click Command1. Change the Number of Copies and Orientation settings in the Print Dialog Box and click OK.

    Result: The line "Copies = 1 & Orientation = 1" is printed in the Debug

               (Immediate window) regardless of the settings you selected in
               the Printer Dialog box.
    
    
NOTE: This same project would produce the expected results on Windows 95. Copies would equal the Number of Copies you selected and the Orientation would be 1 if you selected Portrait and 2 if you selected Landscape.
Keywords          : VB4ALL VB4WIN vb5all VBKBPrinting kbprint
Version           : WINDOWS:4.0,5.0
Platform          : NT WINDOWS
Issue type        : kbprb


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 19, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.