Printing from an Application

See Also

Visual Basic provides three techniques for printing text and graphics.

This section examines the advantages and disadvantages of these three approaches.

Using the PrintForm Method

The PrintForm method sends an image of the specified form to the printer. To print information from your application with PrintForm, you must first display that information on a form and then print that form with the PrintForm method. The syntax is as follows:

[form.]PrintForm

If you omit the form name, Visual Basic prints the current form. PrintForm prints the entire form, even if part of the form is not visible on the screen. If a form contains graphics, however, the graphics print only if the form’s AutoRedraw property is set to True. When printing is complete, PrintForm calls the EndDoc method to clear the printer.

For example, you could send text to a printer by printing it on a form and then calling PrintForm with the following statements:

Print "Here is some text."
PrintForm

The PrintForm method is by far the easiest way to print from your application. Because it may send information to the printer at the resolution of the user’s screen (typically 96 dots per inch), results can be disappointing on printers with much higher resolutions (typically 300 dots per inch for laser printers). The results may vary depending on objects on your form.

For More Information   See "PrintForm Method" in the Language Reference.

Using the Printers Collection

The Printers collection is an object that contains all the printers that are available on the operating system. The list of Printers are the same as those available in the Print Setup dialog box or the Windows Control Panel. Each printer in the collection has a unique index for identification. Starting with 0, each printer in the collection can be referenced by its number.

Regardless of which printing method you use, all printed output from a Visual Basic application is directed to the Printer object, which initially represents the default printer specified in the Windows Control Panel. However, you can set the default printer to any one member in the Printers collection.

To select the printer from the collection, use the following syntax:

Set Printer = Printers(n)

The following statements print the device names of all the printers on the operating system to the Immediate window:

Private Sub Command1_Click()
Dim x As Printer
   For Each x In Printers
      Debug.Print x.DeviceName
   Next
End Sub

Note   You cannot create new instances of the Printer object in code, and you cannot directly add or remove printers from the Printers collection. To add or remove printers on your system, use the Windows Control Panel.

Using the Printer Object

The Printer object is a device-independent drawing space that supports the Print, PSet, Line, PaintPicture, and Circle methods to create text and graphics. You use these methods on the Printer object just as you would on a form or picture box. The Printer object also has all the font properties described earlier in this chapter. When you finish placing the information on the Printer object, you use the EndDoc method to send the output to the printer. When applications close, they automatically use the EndDoc method to send any pending information on the Printer object.

The Printer object provides the best print quality across a variety of printers because Windows translates text and graphics from the device-independent drawing space of the Printer object to best match the resolution and abilities of the printer. You can also print multiple-page documents by using the NewPage method on the Printer object.

The main drawback to using the Printer object is the amount of code required to get the best results. Printing bitmaps on the Printer object also takes time and can therefore slow the performance of the application.