Microsoft KB Archive/322710

= HOW TO: Raise and Control Print Dialog Boxes from Visual Basic =

Article ID: 322710

Article Last Modified on 3/29/2007

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition

-



This article was previously published under Q322710



IN THIS TASK
SUMMARY Download the Control Install Vbprndlg.exe Printerdlg Class Documentation
 * Dialog Box Properties
 * Dialog Box Control Methods and Properties

Steps to Build the Sample REFERENCES



SUMMARY
You can use the Vbprndlg.dll file instead of the Print dialog box portion of the Visual Basic CommonDialog control. This file exposes properties and provides functionality that the CommonDialog control does not.

The CommonDialog control in Visual Basic permits the Print dialog box to be displayed, but it does not give access to many of the properties that can be set in the Print dialog box and consumed by the Printer object. Because of alignment problems with structures (that is, user-defined types) when the Win32 application programming interface (API) functions are made from Visual Basic, you cannot reliably obtain additional information by using these API calls. As a workaround, this article provides a DLL that you can use instead of the Printer portion of the CommonDialog control.

back to the top

Download the Control
The following file is available for download from the Microsoft Download Center:

Vbprndlg.exe

Release Date: Sep-30-2002

For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:

119591 How to Obtain Microsoft Support Files from Online Services

Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help to prevent any unauthorized changes to the file. The Vbprndlg.exe file contains the following files:

back to the top

Install Vbprndlg.exe
 Extract the files to a location that will not be deleted and that has an simple path. These types of files are typically put in the System32 directory under the Windows or the Winnt directory. Click Start, and then click Run. Type the following, and then click OK:

RegSvr32 C:\Windows\System32\VBPrnDlg.dll

Make sure that you use the actual full path to the DLL as the second part of this command.

You receive a message that the DLL successfully registered.

back to the top

Printerdlg Class Documentation
This DLL is a COM component that is added as a reference to a Visual Basic application. After you add the DLL, you can use it in code to display the Print dialog box and to retrieve the information that the user selects. This behavior is the same as the behavior of the Print dialog box in the CommonDialog control in Visual Basic.

back to the top

Dialog Box Properties
For documentation about any of these properties, see the documentation for the CommonDialog control. Printer.hdc is read-only, but all the following properties except those that are marked with an asterisk (*) directly map onto the Printer object when you use the following syntax: Printer.Copies = printDlg.Copies
 * Copies
 * Orientation
 * ColorMode
 * DriverName
 * Duplex
 * PaperBin
 * PaperSize
 * Port
 * PrintQuality
 * hDC
 * PrinterName *
 * Min *
 * Max *
 * FromPage *
 * ToPage *

PrinterName maps to DeviceName in the Printer object. The last four properties that are marked with an asterisk (*) are used to determine if a subset of the available pages must be printed. To enable the Select Pages portion of the Print dialog box, Max must be set to a number that is larger than Min. If Min is larger than Max, an error occurs when the dialog box appears. If the user selects a subset, the PrinterDlg object makes sure that it is inside the bounds of Min and Max, and it is returned in the FromPage and ToPage properties. These properties do not directly map onto the Printer object, so the program that raises the dialog box and controls the printing must handle these properties.

back to the top

Dialog Control Methods and Properties
  Flags - This is the same as the Flags property on the CommonDialog control. It controls some of the features of the dialog box. This value is set using &quot;Or&quot; with different constants in the VBPrinterConstants structure. For example: printDlg.Flags = VBPrinterConstants.cdlPDDisablePrintToFile _ Or VBPrinterConstants.cdlPDNoPageNums These constants are: Constant                 Value       Description --- cdlPDAllPages            &H0         Returns or sets the state of the All Pages option button.

cdlPDCollate             &H10        Returns or sets the state of the Collate check box.

cdlPDDisablePrintToFile  &H80000     Disables the Print To File check box.

cdlPDHelpButton          &H800       Causes the dialog box to display the Help button.

cdlPDHidePrintToFile     &H100000    Hides the Print To File check box.

cdlPDNoPageNums          &H8         Disables the Pages option button and the associated edit control.

cdlPDNoSelection         &H4         Disables the Selection option button.

cdlPDNoWarning           &H80        Prevents a warning message from being displayed when there is no default printer.

cdlPDPageNums            &H2         Returns or sets the state of the Pages option button.

cdlPDPrintSetup          &H40        Causes the system to display the Print Setup dialog box instead of                                      the Print dialog box.

cdlPDPrintToFile         &H20        Returns or sets the state of the Print To File check box.

cdlPDReturnDC            &H100       Returns a device context for the printer selection made in the dialog box. The device context is returned in the hDC property of the dialog box.

cdlPDReturnDefault       &H400       Returns the default printer name.

cdlPDReturnIC            &H200       Returns an information context for the printer selection made in the dialog box. An information context provides a fast way to get information about the device without creating a device context. The information context is returned in                                      the hDC property of the dialog box.

cdlPDSelection           &H1         Returns or sets the state of the Selection option button. If neither cdlPDPageNums nor cdlPDSelection is                                      specified, the All option button is                                       in the selected state.

cdlPDUseDevModeCopies    &H40000     If a printer driver does not support multiple copies, setting this flag disables the Number of Copies spinner control in the Print dialog box. If a                                     driver does support multiple copies, setting this flag indicates that the dialog box stores the requested number of copies in the Copies property.

You can use the Flags property to determine if the user chose a field such as Print to File. To do this, look to see if the cdlPDPrintToFile flag has been set when the control has returned from displaying the dialog box.

 CancelError - This is a Boolean flag that determines whether an error is returned to the program if the user clicks Cancel. By default, this is False, so an error does not occur if the user clicks Cancel. When this is set to True, error number 32755 (&H7FF3) occurs when the user clicks Cancel. You can trap this error to handle a case in which the user cancels the dialog box.</li>  ShowPrinter - This is the only method in this class. Calling this method displays the dialog box. This accepts the handle of the calling window as a parameter. For example, the following method returns a Boolean: printDlg.ShowPrinter Me.hWnd This can be ignored. The value is True if the user clicks Print and False otherwise (that is, if the user clicks Cancel). </li></ul>

NOTE: In all these examples, printDlg is an instance of the class that is contained in this DLL. It is declared by using the following code: Dim printDlg As PrinterDlg Set printDlg = New PrinterDlg back to the top

Steps to Build the Sample
To create a sample application that will use the DLL to determine the user's preferences and then write them to the Debug window, follow these steps:  In Microsoft Visual Basic, create a new Standard EXE project. By default, Form1 is created.</li> On the Project menu, click References.</li> Select Microsoft VB Printer Dialog(PSS), and then click OK.</li> Add a CommandButton control to the form. By default, Command1 is created.</li> Double-click Command1.</li>  Paste the following code in the Command1_Click event: Dim printDlg As PrinterDlg Set printDlg = New PrinterDlg ' Set the starting information for the dialog box based on the current ' printer settings. printDlg.PrinterName = Printer.DeviceName printDlg.DriverName = Printer.DriverName printDlg.Port = Printer.Port

' Set the default PaperBin so that a valid value is returned even ' in the Cancel case. printDlg.PaperBin = Printer.PaperBin

' Set the flags for the PrinterDlg object using the same flags as in the ' common dialog control. The structure starts with VBPrinterConstants. printDlg.Flags = VBPrinterConstants.cdlPDNoSelection _ Or VBPrinterConstants.cdlPDNoPageNums _ Or VBPrinterConstants.cdlPDReturnDC Printer.TrackDefault = False

' When CancelError is set to True the ShowPrinterDlg will return error ' 32755. You can handle the error to know when the Cancel button was ' clicked. Enable this by uncommenting the lines prefixed with &quot;'**&quot;. '**printDlg.CancelError = True

' Add error handling for Cancel. '**On Error GoTo Cancel If Not printDlg.ShowPrinter(Me.hWnd) Then Debug.Print &quot;Cancel Selected&quot; Exit Sub End If

'Turn off Error Handling for Cancel. '**On Error GoTo 0 Dim NewPrinterName As String Dim objPrinter As Printer Dim strsetting As String

' Locate the printer that the user selected in the Printers collection. NewPrinterName = UCase$(printDlg.PrinterName) If Printer.DeviceName <> NewPrinterName Then For Each objPrinter In Printers If UCase$(objPrinter.DeviceName) = NewPrinterName Then Set Printer = objPrinter End If   Next End If

' Copy user input from the dialog box to the properties of the selected printer. Printer.Copies = printDlg.Copies Printer.Orientation = printDlg.Orientation Printer.ColorMode = printDlg.ColorMode Printer.Duplex = printDlg.Duplex Printer.PaperBin = printDlg.PaperBin Printer.PaperSize = printDlg.PaperSize Printer.PrintQuality = printDlg.PrintQuality

' Display the results in the immediate (Debug) window. ' NOTE: Supported values for PaperBin and Size are printer specific. Some ' common defaults are defined in the Win32 SDK in MSDN and in Visual Basic. ' Print quality is the number of dots per inch. With Printer Debug.Print .DeviceName If .Orientation = 1 Then strsetting = &quot;Portrait. &quot; Else strsetting = &quot;Landscape. &quot; End If   Debug.Print &quot;Copies = &quot; & .Copies, &quot;Orientation = &quot; & _ strsetting If .ColorMode = 1 Then strsetting = &quot;Black and White. &quot; Else strsetting = &quot;Color. &quot; End If   Debug.Print &quot;ColorMode = &quot; & strsetting If .Duplex = 1 Then strsetting = &quot;None. &quot; ElseIf .Duplex = 2 Then strsetting = &quot;Horizontal/Long Edge. &quot; ElseIf .Duplex = 3 Then strsetting = &quot;Vertical/Short Edge. &quot; Else strsetting = &quot;Unknown. &quot; End If   Debug.Print &quot;Duplex = &quot; & strsetting Debug.Print &quot;PaperBin = &quot; & .PaperBin Debug.Print &quot;PaperSize = &quot; & .PaperSize Debug.Print &quot;PrintQuality = &quot; & .PrintQuality If (printDlg.Flags And VBPrinterConstants.cdlPDPrintToFile) = _ VBPrinterConstants.cdlPDPrintToFile Then Debug.Print &quot;Print to File Selected&quot; Else Debug.Print &quot;Print to File Not Selected&quot; End If   Debug.Print &quot;hDC = &quot; & printDlg.hDC End With Exit Sub '**Cancel: '**If Err.Number = 32755 Then '**   Debug.Print &quot;Cancel Selected&quot; '**Else '**   Debug.Print &quot;A nonCancel Error Occured - &quot;; Err.Number '**End If                   </li> Press the F5 key to start debugging and to start the application. Click Command1 to open the Print dialog box.</li> After you return from the Print dialog box, look in the Debug window at the output that was returned.</li></ol>

NOTE: If you want a specific error to be raised and handled when the user clicks Cancel (similar to the behavior that occurs with the CommonDialog control), uncomment the lines that are prefixed with &quot;'**&quot;.

back to the top

<div class="references_section">