How to Get Printer Information Using VB6
By Kevin Lee
If your business applications use Microsoft’s VB6 programming language, they may need to communicate with your printers. Making data move from a computer to a printer is complex, but Windows handles most of those chores for you. In fact, you can access printer properties such as orientation and color mode by writing a few lines of VB6 code. All you need to do is learn to create a VB6 printer object.
Launch VB6 and double-click “Standard Exe” to create a new project and add a form to the form editor.
Move to the Toolbox window and click the "CommandButton" control to select. It. Click inside the form, hold down your left mouse button and drag your mouse to draw a small button on the form.
Double-click the button you placed on the form to view the Code window. VB6 positions the cursor inside a new Sub that runs when someone clicks the button. Paste the following code into the Sub at the cursor position:
Dim printerObject As Printer Dim default As String Dim duplex As String Dim orientation As String Dim colorMode As String Dim pages As String
These statements define variables that will contain printer properties. The default variable, for instance, will hold the name of your default printer while the pages property will contain the name of the number of pages that your program has printed since the application issued an EndDoc statement or since the application started.
Add the code shown below after the code listed in the previous section:
default = Printer.DeviceName duplex = Printer.duplex orientation = Printer.orientation colorMode = Printer.colorMode pages = Printer.Page
These statements assign the printer property values to the variables you created. For example, the second statement sets the duplex property that shows whether the printer is set to print on both sides of a page.
Paste the final block of code listed below after the code shown in the last example:
MsgBox "Default printer=" & default & " Duplex= " & duplex & " Orientation=" & orientation & " Color Mode= " & colorMode & " Pages=" & pages
Printer.orientation = vbPRORPortrait MsgBox "Orientation = " & Printer.orientation
Printer.orientation = vbPRORLandscape MsgBox "Orientation = " & Printer.orientation End Sub
The first statement opens a message box that displays the printer properties you set. The next statement sets the printer’s orientation to portrait and opens the message box to show you that the orientation is portrait. The statement after the MsgBox statement sets the printer’s orientation to landscape and displays the orientation in the message box.
Press “F5” to run the project and view the form. Click the form’s button to run the code. It executes, retrieves the form’s printer properties and displays them in the message boxes.
These examples show that you can not only view information about a printer but set the printer’s properties as well by assigning values to the properties as shown.
Many other useful printer properties exist that can help you manage your printer. View them at Microsoft’s Printer Object for Visual Basic 6.0 Users Web page.
After majoring in physics, Kevin Lee began writing professionally in 1989 when, as a software developer, he also created technical articles for the Johnson Space Center. Today this urban Texas cowboy continues to crank out high-quality software as well as non-technical articles covering a multitude of diverse topics ranging from gaming to current affairs.