Visual Basic Concepts

Using the Print Dialog Box

See Also

The Print dialog box allows the user to specify how output should be printed. The user can specify a range of pages to be printed, a print quality, a number of copies, and so on. This dialog box also displays information about the currently installed printer and allows the user to configure or reinstall a new default printer.

Note   This dialog box does not actually send data to a printer. It allows users to specify how they want data printed. You must write code to print the data in the format they select.

For More Information   See "Working with Text and Graphics" for information on printing data.

At run time, when the user makes selections in the Print dialog box, the following properties contain information about the user's selection.

Property Determines
Copies The number of copies to print.
FromPage The page to start printing.
ToPage The page to stop printing.
hDC The device context for the selected printer.
Orientation The page's orientation (portrait or landscape).

Figure 7.15   The Print dialog box

To display the Print dialog box

  1. Set any desired default settings for the dialog by setting the appropriate Print dialog properties.

    For example, to display 2 in the Copies box when the dialog is displayed, set the Copies property to 2:

    CommonDialog1.Copies = 2
    
  2. Use the ShowPrinter method to display the Print dialog box.

The following code displays the Print dialog box when the user clicks the Command1 command button:

Private Sub Command1_Click ()
   Dim BeginPage, EndPage, NumCopies, Orientation, i
   ' Set Cancel to True.
   CommonDialog1.CancelError = True
   On Error GoTo ErrHandler
   ' Display the Print dialog box.
   CommonDialog1.ShowPrinter
   ' Get user-selected values from the dialog box.
   BeginPage   = CommonDialog1.FromPage
   EndPage       = CommonDialog1.ToPage 
   NumCopies   = CommonDialog1.Copies
   Orientation = CommonDialog1.Orientation
   For i = 1 to NumCopies   
   ' Put code here to send data to your printer.
   Next
   Exit Sub
ErrHandler:
   ' User pressed Cancel button.
   Exit Sub
End Sub

Note   If the PrinterDefault property is set to True, you can print to the Visual Basic Printer object. In addition, when the PrinterDefault property is True, any changes the user makes in the Setup portion of the Print dialog box are used to change the printer settings in the user's Printer setup.