List printers in a listbox (1 Viewer)

mafhobb

Registered User.
Local time
Yesterday, 23:30
Joined
Feb 28, 2006
Messages
1,245
Hello,

How can I list the printers installed on the computer in a listbox?

The following code I got from here a while ago (thanks!) list them in a message box, but I need the user to be able to select one to save it in a table. The listbox is called lstListPrinters and the table tblPrinterSelection

Code:
    If Printers.Count > 0 Then

        ' Get count of installed printers.
        strMsg = "Printers installed: " & Printers.Count & vbCrLf & vbCrLf
     
        ' Enumerate printer system properties.
        For Each prtLoop In Application.Printers
            With prtLoop
                strMsg = strMsg _
                    & "Device name: " & .DeviceName & vbCrLf _
                    & "Driver name: " & .DriverName & vbCrLf _
                    & "Port: " & .Port & vbCrLf & vbCrLf

            End With
        Next prtLoop
     
    Else
        strMsg = "No printers are installed."
    End If
 

pr2-eugin

Super Moderator
Local time
Today, 05:30
Joined
Nov 30, 2011
Messages
8,494
You would first create a form with one list box, for now just call it "List0", then instead of adding to the String, you simply use the .AddItem method, that would add items to the list box. Something like,
Code:
    If Printers.Count > 0 Then
       [COLOR=Green] ' Get count of installed printers.[/COLOR]
        strMsg = "Printers installed: " & Printers.Count & vbCrLf & vbCrLf
     
        [COLOR=Green]' Enumerate printer system properties.[/COLOR]
        For Each prtLoop In Application.Printers
            With prtLoop
                Me.List0.AddItem "Device name: " & .DeviceName & ";" _
                               & "Driver name: " & .DriverName & ";" _
                               & "Port: " & .Port 
            End With
        Next prtLoop
    Else
        MsgBox "No printers are installed."
    End If
Just make sure your listbox has three columns, and this code goes into Form_Load()
 

Users who are viewing this thread

Top Bottom