code to ask user to pick a printer

User avatar
stuck
Panoramic Lounger
Posts: 8234
Joined: 25 Jan 2010, 09:09
Location: retirement

code to ask user to pick a printer

Post by stuck »

I have simple line that says:

Code: Select all

ActivePrinter = "\\servername\printername"
but now it needs to be something that asks the user to pick the 'printername' they want.

How could I do that?

Ken

User avatar
StuartR
Administrator
Posts: 12653
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: code to ask user to pick a printer

Post by StuartR »

Here is some code to give you a list of available printers. You could use this to populate a dropdown list for the user to select a printer.
StuartR


User avatar
stuck
Panoramic Lounger
Posts: 8234
Joined: 25 Jan 2010, 09:09
Location: retirement

Re: code to ask user to pick a printer

Post by stuck »

Hmm, do I really need that compexity to offer the user an input box with a drop down containing only two choices? If I make them type the printer name I can get away with only:

Code: Select all

 pname = InputBox("type in either name1 OR name2", "Set the A3 printer")
    ActivePrinter = "\\servername\" & pname
Ken

User avatar
HansV
Administrator
Posts: 78849
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: code to ask user to pick a printer

Post by HansV »

You could create a userform with a combo box populated in the UserFormInitialize event.

Or still simpler, use a message box:

Code: Select all

    Dim pName As string
    Select Case MsgBox("Do you want to print to name1?", vbQuestion + vbYesNo)
        Case vbYes
            pName = "name1"
        Case vbNo
            pName = "name2"
    End Select
    ActivePrinter = "\\servername\" & pName
Best wishes,
Hans

User avatar
stuck
Panoramic Lounger
Posts: 8234
Joined: 25 Jan 2010, 09:09
Location: retirement

Re: code to ask user to pick a printer

Post by stuck »

HansV wrote:Or still simpler, use a message box
Simple is good, that does the trick nicely.

:thankyou:

Ken

User avatar
John Gray
PlatinumLounger
Posts: 5439
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: code to ask user to pick a printer

Post by John Gray »

Just for my amusement, is what you are doing changing the default printer from within Word?
Otherwise, why aren't you just letting the user choose from the printers window when you click on File -> Print (or similar)?

I ask because in my Logon Script, I look up a table of the locations of the PCs, and connect networked printers (using CON2PRT) based on "printer adjacency", with the closest B&W laser printer being assigned as the default for that PC.
John Gray

If you are having problems with solitude, you are not alone.

User avatar
stuck
Panoramic Lounger
Posts: 8234
Joined: 25 Jan 2010, 09:09
Location: retirement

Re: code to ask user to pick a printer

Post by stuck »

I use this code at the start of a macro so that the user can choose the (A3 capable) printer that is nearest to them before the rest of macro goes on prepare a file for printing on A3 paper.

Yes, the effect of this code is to change the default printer in Word but we can live with that for now. I will get round to a more elegant solution eventually.

Ken

User avatar
John Gray
PlatinumLounger
Posts: 5439
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: code to ask user to pick a printer

Post by John Gray »

Thanks, Ken - that's more variable than my situation!
John Gray

If you are having problems with solitude, you are not alone.

User avatar
Guessed
2StarLounger
Posts: 103
Joined: 04 Feb 2010, 22:44
Location: Melbourne Australia

Re: code to ask user to pick a printer

Post by Guessed »

There is a simple one-liner to display a built-in Word dialog where you can change the default printer.

Dialogs(97).Show

As far as I can tell, this dialog is undocumented but it has been kicking around in old versions of Word for well over 10 years and still works in Word 2010.
Andrew Lockton
Melbourne Australia

User avatar
StuartR
Administrator
Posts: 12653
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: code to ask user to pick a printer

Post by StuartR »

Guessed wrote:...
Dialogs(97).Show
...
That could be very useful, thank you for sharing it.
StuartR


User avatar
stuck
Panoramic Lounger
Posts: 8234
Joined: 25 Jan 2010, 09:09
Location: retirement

Re: code to ask user to pick a printer

Post by stuck »

Neat, that could be handy, thanks.

Ken