Link to home
Start Free TrialLog in
Avatar of Shane_CU
Shane_CU

asked on

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

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:
http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/q166/0/08.asp&NoWebContent=1
(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?
Avatar of vinnyd79
vinnyd79

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
Next
End Sub
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
Next
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
Avatar of Shane_CU

ASKER

I am using VBA in Excel.  "Printer" does not appear to be a defined type.
ASKER CERTIFIED SOLUTION
Avatar of vinnyd79
vinnyd79

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
vinny,
can't get that link to work
nevermind.  got it working.
Avatar of aelatik
'try scripting object

    Dim WSNET, WSPRN As Object, I As Long
    Set WSNET = CreateObject("wscript.network")
    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
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!