Visual Basic Code of the Week (COTW)
Issue #100
Online Version at (paid subscribers only)
All content and source code is Copyright (c) 1999 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp.

This week's topic: Change printers programmatically

Thank you! / VB Training / Get paid to surf the web!

We would like to thank all our subscribers for supporting us for the last 100 issues of Code of the Week. We hope you stay with us for the next 100 and more!

Want to get up to speed on the latest Visual Basic programming? Check out our training programs at

If you would like to get paid for surfing the web, jump to


In this Issue

This issue introduces a class module which shows how to change the printer Visual Basic currently prints its output to and provide a way to easily change the document name which shows in the printer queue.

If you have any questions about using this class, let us know at


Public Property Let DocumentName(sDocName As String)

Sets the document name to appear in the print queue under Windows. By default this is the Project Name.

Public Property Get DocumentName() As String

Retrieves the document name to appear in the print queue under Windows. By default this is the Project Name.


Public Function IsPrinterReady() As Boolean

Determines if the printer is ready to print to. This is not 100% accurate, but it was the best we could come up with. If anyone has a more reliable way to determine this, please let us know and we will update this class.


Public Sub LoadPrinterCombo(cmb As ComboBox)

Load all the installed printer names into a combo box.

Public Sub SetPrinterByName(sName As String)

Sets the current Printer object to the printer name specified by sName. If the printer name is not found it will raise an error.

Public Sub StartJob()
Public Sub EndJob()

If you want to use the DocumentName feature of this class you will need to call the StartJob method before sending anything to the printer. When you are through with the print job, call EndJob. EndJob will also perform the EndDoc so you will not need to call it yourself. By using these methods this printer class will manage the DocumentName correctly.


See above descriptions.

Sample Usage

This sample will shows a sample printer selection form. It assumed you have added a combo box called cmbPrinters and a command button called cmdPrint and a textbox called txtDocName (which contains the name of the document you are printing).

Dim ptr As cPrinters

Private Sub cmbPrinter_Click()
    ptr.SetPrinterByName cmbPrinter.Text
End Sub

Private Sub cmdPrint_Click()
    If ptr.IsPrinterReady Then
        ptr.DocumentName = txtDocName.Text
        On Error GoTo Handler
        Printer.Print "test printout1."
        ' print more stuff here.
        MsgBox "Print job has completed."
        MsgBox "Could not print to " & cmbPrinter.Text
    End If
    Exit Sub

    MsgBox "An error occurred while trying to print the document.  The document will be removed from the print queue."
End Sub

Private Sub Form_Load()
    Set ptr = New cPrinters
    ptr.LoadPrinterCombo cmbPrinter
End Sub

Source Code

To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address:

This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.

Other links

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945