Using VBA Code to Detect a Printer on the Computer

If you want to use a Macro script in PowerPoint to automatically print a slide to a specific printer that is not the default printer, or if you want to detect if a certain printer name is present in the system, then you can use the following VBA Macro to perform this task.

The Macro works well in Microsoft Office for Windows by performing a system call and returning a value that compared with the printer name will allow the user to know if the printer is present or not in the system.

Private Declare Function CreateIC Lib "gdi32" Alias "CreateICA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, ByVal lpInitData As Long) As Long

Public Function GetPrinter(ByVal strPrinterName As String) As Long
    GetPrinter = CreateIC("WINSPOOL", strPrinterName, vbNullString, 0&)
End Function

Sub SearchPrinter()
    Dim printerName As String
    printerName = "Enter printer name here"
    'printerName = "Fax"
    If GetPrinter(printerName) = 0 Then
        MsgBox ("Printer " & printerName & " was not found")
    Else
        MsgBox ("Printer " & printerName & " was found OK!")
    End If
End Sub

Make sure to replace the printerName variable with the name of the printer that you want to compare. Depending on your needs, you’d need to adjust the script accordingly.

For instance, if your printer options look as follow:

VBA Choose Printer Macro

Then, you’d replace the printerName variable by “Microsoft Print to PDF” and once the SearchPrinter macro is executed, it will display “Printer Microsoft Print to PDF was found OK!”

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...

Leave a Comment

Your email address will not be published. Required fields are marked *