Solved

Print to selected printer

Posted on 2014-10-11
9
167 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 47

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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 47

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 47

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 47

Expert Comment

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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to make an ADE file by code? 11 110
vb6 - Transfer from MSHFlexgrid1 to xls issue 8 66
VBA: Insert New column with specific format type 12 56
Problem to line 23 80
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…

751 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