[Excel VBA] How to Get a List of Installed Printers on a PC

When creating print-related macros in VBA, it is often necessary to list currently available printer names or check if a specific printer is installed.

By accessing Windows Shell functions from VBA, a list of printers registered on the PC can be easily obtained. This article introduces a macro that uses the Shell.Application object to retrieve the names of all installed printers and output them to the Immediate Window.

目次

VBA Sample Code to Get a List of Installed Printers

This macro accesses the “Printers” folder, which is a Windows system folder, and retrieves the names of all items (printers) contained within it one by one.

Completed Code

' Get all printer names installed on the PC
Sub ListInstalledPrinters()

    '== Define variables ==
    Dim shellApp As Object
    Dim printerFolderItems As Object
    Dim printerItem As Object
    
    '--- Create Shell.Application object ---
    Set shellApp = CreateObject("Shell.Application")
    
    '--- Get the "Printers" folder object ---
    ' &H4 is a special constant pointing to the "Printers" folder
    Set printerFolderItems = shellApp.Namespace(&H4).Items
    
    '--- Output to Immediate Window ---
    Debug.Print "--- Installed Printer List ---"
    
    For Each printerItem In printerFolderItems
        Debug.Print printerItem.Name
    Next printerItem
    
    '--- Release objects ---
    Set shellApp = Nothing
    
    MsgBox "Printer list has been output to the Immediate Window."

End Sub

Explanation of the Code

Set shellApp = CreateObject("Shell.Application")

The CreateObject function is used to generate a Shell.Application object. This allows VBA to manipulate the Windows Shell (UI functions such as Explorer).

shellApp.Namespace(&H4).Items

This single line is the core part for accessing the printers folder.

  • .Namespace(...): A method for accessing special Windows folders (Desktop, Recycle Bin, Control Panel, etc.).
  • &H4: The value &H4 (hexadecimal 4) passed as an argument is a special constant value that points specifically to the “Printers” folder.
  • .Items: Returns a collection of all items contained within the retrieved folder object.

For Each printerItem In printerFolderItems

The retrieved printerFolderItems collection is processed one by one using a For Each loop. Each printerItem becomes an object representing an individual printer.

Debug.Print printerItem.Name

The .Name property of the printerItem object contains the printer name (e.g., “Microsoft Print to PDF”). This is output to the Immediate Window.

Summary

This article explained how to retrieve a list of installed printers by utilizing Windows Shell functions from VBA.

  1. Generate a shell object with CreateObject("Shell.Application").
  2. Access the “Printers” folder with .Namespace(&H4).
  3. Loop through the .Items in the folder and retrieve the .Name of each item.

Using this method enables verification of available printer names or checking for the existence of a specific printer before setting Application.ActivePrinter, allowing for the creation of more advanced and stable printing macros.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次