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?
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?
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
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
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
ASKER
I am using VBA in Excel. "Printer" does not appear to be a defined type.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
vinny,
can't get that link to work
can't get that link to work
ASKER
nevermind. got it working.
'try scripting object
Dim WSNET, WSPRN As Object, I As Long
Set WSNET = CreateObject("wscript.netw ork")
Set WSPRN = WSNET.EnumPrinterConnectio ns
For I = 0 To WSPRN.Count - 1 Step 2
MsgBox WSPRN(I) & WSPRN(I + 1)
Next I
Set WSPRN = Nothing
Set WSNET = Nothing
Dim WSNET, WSPRN As Object, I As Long
Set WSNET = CreateObject("wscript.netw
Set WSPRN = WSNET.EnumPrinterConnectio
For I = 0 To WSPRN.Count - 1 Step 2
MsgBox WSPRN(I) & WSPRN(I + 1)
Next I
Set WSPRN = Nothing
Set WSNET = Nothing
ASKER
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!
Private Sub Command1_Click()
Dim Prn As Printer
For Each Prn In Printers
List1.AddItem Prn.DeviceName & vbTab & Prn.Port
Next
End Sub