Setting Activeprinter (my problem is after "on" in the printer name)

Posted on 2003-10-28
Last Modified: 2008-02-01
I have a drop down list that is populated with the names of all printers installed on the computer. I did this using a slightly modified version of the enumerate printers method microsoft describes here:
(I am using the PName strings to obtain the printer names)

The goal is to set the ActivePrinter to the one the user chooses from the list. My problem is that the enumerate printers method does not return the name of the printers' ports; thus, I cannot properly assign the activeprinter because I cannot complete the "on [port name]" portion.

Is there any way to retrieve the port names along with the printer names?
Question by:Shane_CU
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
LVL 28

Expert Comment

ID: 9635816
Have you tried the printer object:

Private Sub Command1_Click()
Dim Prn As Printer
For Each Prn In Printers
    List1.AddItem Prn.DeviceName & vbTab & Prn.Port
End Sub
LVL 28

Expert Comment

ID: 9635867
You could use something like this to change the active printer for your app:

Private Sub Form_Load()
Dim Prn As Printer
For Each Prn In Printers
    List1.AddItem Prn.DeviceName
End Sub

Private Sub List1_Click()
SelectPrinter List1.List(List1.ListIndex)
MsgBox List1.List(List1.ListIndex) & " has been selected!"
End Sub

Private Function SelectPrinter(ByVal printer_name As String) As Boolean
Dim i As Integer
SelectPrinter = True
    For i = 0 To Printers.Count - 1
        If Printers(i).DeviceName = printer_name Then
            Set Printer = Printers(i)
            SelectPrinter = False
            Exit For
        End If
    Next i
End Function

Author Comment

ID: 9635869
I am using VBA in Excel.  "Printer" does not appear to be a defined type.
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

LVL 28

Accepted Solution

vinnyd79 earned 160 total points
ID: 9635905

Author Comment

ID: 9635977
can't get that link to work

Author Comment

ID: 9635985
nevermind.  got it working.
LVL 14

Expert Comment

ID: 9636030
'try scripting object

    Dim WSNET, WSPRN As Object, I As Long
    Set WSNET = CreateObject("")
    Set WSPRN = WSNET.EnumPrinterConnections
    For I = 0 To WSPRN.Count - 1 Step 2
        MsgBox WSPRN(I) & WSPRN(I + 1)
    Next I
    Set WSPRN = Nothing
    Set WSNET = Nothing

Author Comment

ID: 9636045
wow.  this is a lot more involved than I initially intended.  But thanks to, VinnyD's link I can get it done.  I am having to use Microsoft's enumerate priters method in combination with the code that VinnyD gave me the link for!

Featured Post

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!

Question has a verified solution.

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

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

730 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