Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Print to selected printer

Posted on 2014-10-11
9
Medium Priority
?
170 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
  • 4
  • 3
  • 2
9 Comments
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 800 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 49

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 49

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 49

Accepted Solution

by:
Martin Liss earned 1200 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 49

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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

783 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