0

I have some code that lists all of the active printers (but does so in msgboxes rather than a list). I have no problem with that functionality. Here is the code below:

    If Printers.Count > 0 Then
    strMsg = "Printers installed: " & Printers.Count & vbCrLf & vbCrLf
    For Each prtLoop In Application.Printers
        With prtLoop
           MsgBox .DeviceName                
      End With
    Next prtLoop
    Else
    MsgBox "No printers are installed."
    End If

While it does what I essentially need it to do I would like it to only popup if the printer that matches a partial of a printer name that is set in the code. For example if I have 3 printers:

Printer ABC 1
Printer ZZ5 2 (copy)
Printer 123

I want there to be an If Then statement probably after the

    For Each prtLoop In Application.Printers

portion of the code above that says if the device name is *ABC then it would only popup with "Printer ABC 1" instead of all 3.

I hope this makes sense. Thanks in advance for the help. -Vladimir

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
vladyerus
  • 37
  • 7

1 Answers1

1

Maybe that is what you wanna do ?
If Printers.Count > 0 Then strMsg = "Printers installed: " & Printers.Count & vbCrLf & vbCrLf For Each prtLoop In Application.Printers With prtLoop If InStr(DeviceName, "ABC") Then MsgBox .DeviceName
End If
End With Next prtLoop Else MsgBox "No printers are installed." End If

edit: Access didn't like the If then statement because it was one line, error was coming up with "End If without Block If" error, I moved the MsgBox .DeviceName to its own line and it works.

vladyerus
  • 37
  • 7
Matthias
  • 86
  • 5
  • Thank you Matthias!!! This works awesome. I did make a small change because Access threw up an error (End if without block if) because of MsgBox .Devicename being on the same line, after I moved it to its own line no more error and it worked fine. – vladyerus Jan 23 '17 at 14:44