Code Samples for Businesses, Schools & Developers

First Published 12 Oct 2022                                 Last Updated 2 Dec 2023


The code below lists all installed physical and virtual printers and identifies the default printer.

Place the code in a standard module.

CODE:

Option Compare Database
Option Explicit

Public Function getDefaultPrinter() As String
      Dim computer As String
      Dim wmiService As Object
      Dim installedPrinters As Variant
      Dim printer As Object
      Dim I As Integer

On Error Resume Next

      'Set the computer. Dot means the computer running the code.
      computer = "."

      'Get the WMI object
      Set wmiService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & computer & "\root\cimv2")

      'Retrieve information about the installed printers (by running a query).
      Set installedPrinters = wmiService.ExecQuery("Select * from Win32_Printer")

      'If an error occurs in the previous step, inform the user.
      If Err.number <> 0 Then
           MsgBox "Could not retrieve the printer information from WMI object!", vbCritical, "WMI Object Error"
           Exit Function
      End If

      'Loop through all the installed printers and get their name. Check if one of them is the default one.
      'Printer status
      'Other (1)
      'Unknown (2)
     'Idle (3)
      'Printing (4)
      'Warmup (5)
      'Stopped Printing(6)
      'Offline (7)

      For Each printer In installedPrinters
            'Write the results to the immediate window
            ' Debug.Print printer.Name, printer.PrinterStatus, printer.Local

           Debug.Print printer.Name, " Local: " & printer.Local & "," & vbTab & " Default: " & printer.Default & _
                 "," & vbTab & " Work Offline: " & printer.workoffline

            If (printer.Default) Then
                  getDefaultPrinter = printer.Name
                  Debug.Print "====================" & vbCrLf & "Default printer = " & getDefaultPrinter
                 'Exit For

            End If
      Next printer

End Function



Example output: (from the Immediate window)

Send To OneNote 2010       Local: True,       Default: False,       Work Offline: False
OneNote for Windows 10       Local: True,       Default: False,       Work Offline: False
OneNote (Desktop)       Local: True,       Default: False,       Work Offline: False
Microsoft XPS Document Writer       Local: True,       Default: False,       Work Offline: False
Microsoft Print to PDF       Local: True,       Default: False,       Work Offline: False
Fax       Local: True,       Default: False,       Work Offline: False
EPSON AL-C1600       Local: True,       Default: True,       Work Offline: True
====================
Default printer = EPSON AL-C1600




Click to download:

The module code is available in the attached (zipped) file.
After unzipping, you can then import the modPrint.bas file directly into the Visual Basic Editor

         modPrint      (bas - zipped)



Related Articles

      Set Default Printer

      Count Jobs in Print Queue



Feedback

Please use the contact form below to let me know whether you found this article interesting/useful or if you have any questions/comments.

Please also consider making a donation towards the costs of maintaining this website. Thank you



Colin Riddington                 Mendip Data Systems                 Last Updated 2 Dec 2023



Return to Code Samples Page




Return to Top