This article describes an ActiveX component that helps programmers to enumerate Windows printers. It's primarily aimed at Visual Basic for Applications (VBA) developers whose VBA implementation contains no built-in way to ask Windows about the printers available to a computer.

Some VBA implementation includes printer tools (Excel VBA, for example) but others lack them, presumably because they are not part of the Microsoft Windows VBA Toolkit provided to OEMs. An example of a VBA that omits printers altogether is Bentley Systems MicroStation VBA.

Q Here are some questions about printers and printer enumeration posted by MicroStation VBA developers on the MicroStation Programming Forum …

A A solution to those questions is our Printer Enumerator ActiveX DLL. It is a COM server which a VBA programmer can use to query printers attached to a computer. The Printer Enumerator is freeware.

Printer Enumerator

The Printer Enumerator is a DLL, also known as an ActiveX component or COM server. It provides a COM interface, which a VBA programmer can discover simply by referencing the DLL in her VBA Interactive Development Environment (IDE).

The primary interface is the Printers API. It tells you how many printers are available and the name of each printer.

Create a Printers object like this …

Dim oPrinters As PrinterEnumerator.Printers
Set oPrinters = New PrinterEnumerator.Printers

Another interface is the Printer class. Obtain a Printer object from Printers like this …

Dim oPrinters As PrinterEnumerator.Printers
Set oPrinters = New PrinterEnumerator.Printers

Dim oPrinter As PrinterEnumerator.Printer
Set oPrinter = oPrinters.Printer ("HP DeskJet")

If a printer having the designated name does not exist then Printer will be Nothing. You can't create a Printer object in any other way. In other words, the following is not possible …

Dim oPrinter As PrinterEnumerator.Printer
Set oPrinter = New PrinterEnumerator.Printer ' Not possible

Example VBA Code

Before you can use a COM component such as the Printer Enumerator you must create a reference to it. Create a reference in the VBA IDE from the Tools|References menu …

VBA References Dialog

If you want to examine the interface in detail, press key F2 to pop the VBA object browser …

VBA Object Browser

The following code is taken from the Excel VBA example delivered with Printer Enumerator …

Public Sub Main()
    Dim oPrinters                           As New PrinterEnumerator.Printers
    Debug.Print oPrinters.ToString
    Dim nPrinters                           As Long
    nPrinters = oPrinters.PrinterCount
    Debug.Print "There are " & CStr(nPrinters) & " printers"
    Dim i                                   As Long
    For i = 0 To nPrinters - 1
        Dim printerName                     As String
        printerName = oPrinters.printerName(i)
        Debug.Print "Printer [" & CStr(i + 1) & "] '" & printerName & "'"
        Dim oPrinter                        As PrinterEnumerator.Printer
        Set oPrinter = oPrinters.Printer(printerName)
        Dim nPaperSizes                     As Long
        nPaperSizes = oPrinter.PaperSizeCount
        Debug.Print "Printer '" & oPrinter.Name & "' has " & CStr(nPaperSizes) & " paper sizes"
        Dim size                            As Long
        For size = 0 To nPaperSizes - 1
            Debug.Print "Size [" & CStr(size) & "] name '" & _
                oPrinter.PaperName(size) & _
                "' kind '" & oPrinter.PaperKind(size) & "'"
        Next size
    Next i
End Sub

Download the Printer Enumerator Component

The Printer Enumerator ActiveX Component is a Windows DLL (PrinterEnumerator.dll). The installer in the ZIP package copies the DLL to your Program Files (x86) folder (on a 32-bit version of Windows it's the Program Files folder) and registers the COM interface with Windows.

After installation you should find the file PrinterEnumerator.dll in folder C:\Program Files (x86)\LA Solutions\bin

After installation you should find the example Excel file PrinterTester.xlsm in folder C:\Program Data\LA Solutions\PrinterEnumerator