Drop down menu to select printers from macro, and wscript.network object

Asator

Board Regular
Joined
Apr 5, 2010
Messages
186
Is there a vba method I can use to populate a popup dropdown menu with array items? I want to allow the user to select a printer from a list generated by the following code:

Code:
Dim objPrinter As Object
Dim arrPrinters() As Variant
Dim prCount As Integer
Set objPrinter = CreateObject("wscript.network")
For Each printer In objPrinter.enumprinterconnections
If (IsNumeric(InStr(printer, "\\")) And InStr(printer, "\\") <> 0) Or ((IsNumeric(InStr(LCase(printer), "pdf"))) And InStr(LCase(printer), "pdf")) Then
    ReDim Preserve arrPrinters(0 To prCount)
    arrPrinters(prCount) = printer
    Debug.Print printer
    prCount = prCount + 1
    
End If
Next printer
Set objPrinter = Nothing

Is that possible?


Also, I know I can then set a new default printer with objPrinter.SetDefaultPrinter.. but is there a way I can return only the default printer in the same format, so that I can store it while the macro runs, then restore it when it completes?
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
http://webcache.googleusercontent.c...d=3&hl=en&ct=clnk&gl=us&source=www.google.com
"you could add an extra line at the end WScript.Echo "Your DEFAULT printer : " & UNCpath1"

That's VB, not VBA. Dunno if something similar would work here, but you can play around with it.

Yes, it should be easy to do the other part of what you want to do. You've got the hard part done already.

Code:
For i = lbound(arrPrinters) to ubound(arrPrinters)
 
   Combobox1.AddItem arrprinters(i)
 
Next

It's also possible to load items from a two dimensional array using the list and column properties, but I've never sat down to figure that out.

See "Ways to put data into a listbox or combobox" in VBA help.
 
Upvote 0

Forum statistics

Threads
1,224,596
Messages
6,179,802
Members
452,943
Latest member
Newbie4296

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top