Print to selected printer

I am trying to select the printer from a listbox and printform.
the code:
Sub LoadPrinters()
    Dim i As Integer
    Dim lCount As Long
    Dim p As Printer
    lCount = Printers.Count
      If lCount = 0 Then
        Call MsgBox("No printer detected on this system", vbExclamation, App.Title)
      End If
       
        For Each p In Printers
         lstPrinters.AddItem p.DeviceName
        Next
           Exit Sub 
ErrHandler:
End Sub

Open in new window

How do i set the print to the listbox selected printer ?
isnoend2001Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Martin LissConnect With a Mentor Older than dirtCommented:
What do you mean? You can do something like this

Private Sub lstPrinters_Click()
   
    Dim p As Printer
    For Each p In Printers
        If p.DeviceName = lstPrinters.Text Then
            Set Printer = p
            Exit For
        End If
    Next p

   PrintForm
   
End Sub
0
 
GrahamSkanConnect With a Mentor RetiredCommented:
Walk through the Printers collection again and match the DeviceName to the List Box selection
Sub SetPrinter()
    Dim p As Printer
       
    For Each p In Printers
        If p.DeviceName = lstPrinters.AddItem Then
            Set Printer = p
            Exit For
        End If
    Next
End Sub

Open in new window

0
 
Martin LissOlder than dirtCommented:
This also sets the listindex of the list to the default printer when the listbox is loaded.

Sub LoadPrinters()
    Dim i As Integer
    Dim lCount As Long
    Dim p As Printer
    Dim intDefaultLI As Integer
    
    lCount = Printers.Count
    If lCount = 0 Then
        Call MsgBox("No printer detected on this system", vbExclamation, App.Title)
    End If
    
    For Each p In Printers
        lstPrinters.AddItem p.DeviceName
        If p.DeviceName = Printer.DeviceName Then
            intDefaultLI = lstPrinters.NewIndex
        End If
    Next
    lstPrinters.ListIndex = intDefaultLI
    
    Exit Sub
ErrHandler:
End Sub

Private Sub lstPrinters_Click()
    
    Dim p As Printer
    For Each p In Printers
        If p.DeviceName = lstPrinters.Text Then
            Set Printer = p
            Exit For
        End If
    Next p
    
End Sub

Open in new window

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
isnoend2001Author Commented:
Thanks GrahamSkan
that gives error: Argument not Optinal on highlighted
If p.DeviceName = lstPrinters.AddItem Then
0
 
Martin LissOlder than dirtCommented:
Did you try my code?
0
 
isnoend2001Author Commented:
MartinLiss Seems it does not give a chance to select from the list
0
 
isnoend2001Author Commented:
Thanks guys
0
 
GrahamSkanRetiredCommented:
Sorry for my transcription error.  I copied the uncorrected version to here.
0
 
Martin LissOlder than dirtCommented:
Glad I could help.
0
All Courses

From novice to tech pro — start learning today.