Solved

Print to selected printer

Posted on 2014-10-11
9
168 Views
Last Modified: 2014-10-11
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 ?
0
Comment
Question by:isnoend2001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 200 total points
ID: 40374836
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
 
LVL 48

Expert Comment

by:Martin Liss
ID: 40374900
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
 

Author Comment

by:isnoend2001
ID: 40374911
Thanks GrahamSkan
that gives error: Argument not Optinal on highlighted
If p.DeviceName = lstPrinters.AddItem Then
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 48

Expert Comment

by:Martin Liss
ID: 40374914
Did you try my code?
0
 

Author Comment

by:isnoend2001
ID: 40374927
MartinLiss Seems it does not give a chance to select from the list
0
 
LVL 48

Accepted Solution

by:
Martin Liss earned 300 total points
ID: 40374935
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
 

Author Closing Comment

by:isnoend2001
ID: 40374967
Thanks guys
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40374968
Sorry for my transcription error.  I copied the uncorrected version to here.
0
 
LVL 48

Expert Comment

by:Martin Liss
ID: 40374971
Glad I could help.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month10 days, 7 hours left to enroll

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question