Solved

Print to selected printer

Posted on 2014-10-11
9
166 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 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
Industry Leaders: 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!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

756 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