Loading Device Names into a Combo Box

The Printers collection consists of a set of Printer objects. Changing the printer associated with the Printer object is as simple as using the Set statement to make the Printer object refer to a specific member of the Printers collection. You can do this by loading all of the DeviceNames into a ComboBox control, as shown in Listing 9.1. Then you can switch printers whenever the user clicks on a new printer DeviceName, as shown in Listing 9.2.

Listing 9.1: LoadPrinters Routine in Printer

Private Sub LoadPrinters()
Dim pr As Printer
For Each pr In Printers
   Combo1.AddItem pr.DeviceName
Next pr
Combo1.Text = Printer.DeviceName
End Sub

Listing 9.2: Combo1_Click Event in Printer

Private Sub Combo1_Click()
Set Printer = Printers(Combo1.ListIndex)
End Sub

Using this approach, you are temporarily defining a printer to be the default printer for this application; you aren’t changing the system default printer. You usually don’t want to reset the default printer for Windows, because whoever selected it probably did so because most of that user’s output is going to be sent there.

© 1998 SYBEX Inc. All rights reserved.