How Can I Let Users Choose a Default Printer From a List of Printers?

How Can I Let Users Choose a Default Printer From a List of Printers?

  • Comments 1
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! In a logon script, how can I enable users to choose a default printer from a list of the printers installed on their computer?

-- JD

SpacerHey, Scripting Guy! AnswerScript Center

Hey, JD. You know, a lot of people write in to say, “Hey, Scripting Guys! Why do you limit yourselves to system administration scripting? Don’t you realize that you guys, and you guys alone, have the power to make the world a better place?”

Now, to be honest, sometimes we – what’s that? How many people write in to say that we, and we alone, have the power to make the world a better place? Well, it’s not really our style to keep count of that sort of thing. Nevertheless, we’re sure that it’s – hey, you know what? Numbers are really meaningless when it comes to stuff like this. The important thing is that even though we maintain a laser-like focus on system administration scripting we can still have an impact, we can still help make the world a better place.

Case in point? Following this year’s Super Bowl we made particular note of one of the commercials aired during the game. In this commercial, a robot that worked for a US auto maker made a simple mistake, and was immediately fired. (Interestingly enough, Microsoft has the exact same policy; fortunately, though, none of the Scripting Guys have ever made a mistake.) After failing at a number of new jobs our robot finds himself (herself?) standing atop a bridge, ready to jump off and end it all. At that very moment, however, he (she?) wakes up and realizes that it was all a bad dream.

As we noted back in February, the Scripting Guys were a little perplexed by the message sent by this commercial. As near as we could tell, the auto maker was telling its employees this: if you make a mistake we’re going to fire you and, when that happens, you might as well just kill yourself. That’s pretty inspiring and uplifting. We thought that was an odd message to send, especially in light of the fact US auto makers have laid off an awful lot of people over the past few years.

Now, we know what you’re thinking: that is an odd message to send, isn’t it? But what can one person do about that, even if that one person is a Scripting Guy? Well, here’s what one person can do about that. Last night the Scripting Guy who writes this column saw the robot commercial for the first time since the Super Bowl. And guess what? The ending has been changed. In the new version the robot doesn’t stand atop a bridge ready to jump off and commit suicide. That can mean only one thing: the auto maker read our Hey, Scripting Guy! column, realized the error of its ways, and changed the commercial.

OK, granted, in the new version the robot sees a similar robot being unceremoniously tossed onto a scrap heap. But this is at least a step in the right direction.

The moral of this story (and yes, all Hey, Scripting Guy! stories have a moral … somewhere) is that one person can make a difference, one person does have the power to make the world a better place. Maybe you can do this by saving a fictional robot from thoughts of suicide. Or, even better, maybe you can do this by coming up with a way for users to choose a default printer from a list of the printers installed on their computer:

<SCRIPT Language="VBScript">

    Sub Window_Onload
        strComputer = "."

        Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
        Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")

        For Each objPrinter in colPrinters
            strPrinter = objPrinter.Name
            Set objOption = Document.createElement("OPTION")
            objOption.Text = strprinter
            objOption.Value = strPrinter
            AvailablePrinters.Add(objOption)
        Next
    End Sub

    Sub SetDefault
        strPrinter = AvailablePrinters.Value
        Set WshNetwork = CreateObject("Wscript.Network")
        WshNetwork.SetDefaultPrinter strPrinter
        Msgbox strprinter & " has been set as your default printer."
    End Sub

</SCRIPT>

<select size="5" name="AvailablePrinters"></select><p>
<input type="button" value="Set as Default" onClick="SetDefault">

Let’s talk about what we have here; what we do have here is an HTA (HTML Application). HTAs are simply a way for script writers to create graphical user interfaces; they are essentially Web pages (albeit with a .hta file extension) designed to run off your local machine. (Yes, we know: that’s not a very good explanation. If you need more information about HTAs, well, that’s what the HTA Developers Center is for.) VBScript doesn’t have a native method for creating graphical user interfaces. That’s a problem, but we can work around that problem by creating an HTA, and then calling that HTA from within our logon script.

And that’s exactly what we’ve done here: we’ve created a .hta file, a file that we’ll need to copy to each of our computers. (But that’s no problem; your logon script can do that.) Inside our logon script we then need just two lines of code to pop up this HTA and, in turn, let users select a default printer:

Set objShell = CreateObject("Wscript.Shell")
objShell.Run "C:\Scripts\Test.hta"

So what will people see when this HTA pops up? They’ll see – in all its barebones splendor – something that looks like this:

HTA


Yes, we know. But if you want beautiful you’re on your own.

Now that you understand the basic scenario let’s take a closer look at the HTA itself. As far as the HTML tagging goes, well, there really isn’t much: all we have is a list box named AvailablePrinters (added to the HTA by using the <SELECT> tag) and a button (<INPUT TYPE = "BUTTON">):

<select size="5" name="AvailablePrinters"></select><p>
<input type="button" value="Set as Default" onClick="SetDefault">

The idea here is that you select a printer from the list box and then click the button; upon doing so, the printer you selected will be configured as your default printer.

That’s right: just like magic.

If you’ve done much HTML tagging you probably noticed that, as configured, our list box is empty; it doesn’t include a single installed printer. Why not? Well remember, that’s the whole idea here: we need a dynamic list box, one that, each time the HTA starts, gets populated with the printers (and only those printers) installed on the computer in question. How do we propose to do that? Why, by using this subroutine, of course:

Sub Window_Onload
    strComputer = "."

    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")

    For Each objPrinter in colPrinters
        strPrinter = objPrinter.Name
        Set objOption = Document.createElement("OPTION")
        objOption.Text = strPrinter
        objOption.Value = strPrinter
        AvailablePrinters.Add(objOption)
    Next
End Sub

This, needless to say, is a subroutine named Window_Onload; in an HTA (or in a Web page) any subroutine with that name automatically runs whenever the HTA is opened or refreshed. Inside this subroutine, we first use this block of code to retrieve a collection of all the printers installed on the local computer:

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")

That was easy, wasn’t it? The question now is this: what do we do with this collection of printers?

To begin with, we set up a For Each loop to loop through each printer in the collection. For each printer we grab the value of the Name property and store it in a variable named strPrinter. We then run headlong into this line of code:

Set objOption = Document.createElement("OPTION")

What we’re doing here is creating an instance of the HTML Option object; as the name implies, this object represents an individual option in a list box. After creating a “blank” option we then assign the printer name to the option’s Text and Value properties:

objOption.Text = strPrinter
objOption.Value = strPrinter

In case you’re wondering, the Text property represents the actual text displayed in the list box; the Value property is, well, the value assigned to that option. For this particular list box we made the Text and Value identical, but they don’t have to be. For example, suppose you had a list box that allowed people to select US states. You could set the Text of an option to Washington; that way people would see the name Washington in the list box. At the same time, however, you could set the value of that option to WA. That way people would see a “friendly” name in the list box, but you could still use the official postal abbreviation in your script.

After we assign the property values we then call the Add method and add the option (that is, the name of the first printer) to the list box:

AvailablePrinters.Add(objOption)

And then we repeat the process with the next printer in the collection.

The Window_Onload subroutine populates our list box with the names of all the printers installed on the local computer. To make one of these printers the default printer we need to select a the printer from the list box and then click the Set as Default button. Clicking the button causes the following subroutine to run:

Sub SetDefault
    strPrinter = AvailablePrinters.Value
    Set WshNetwork = CreateObject("Wscript.Network")
    WshNetwork.SetDefaultPrinter strPrinter
    Msgbox strPrinter & " has been set as your default printer."
End Sub

As you can see, we’re not doing anything too-terribly fancy in this subroutine. To begin with, we grab the Value from the item selected in the list box and assign that to a variable named strPrinter:

strPrinter = AvailablePrinters.Value

We use these two lines of code to create an instance of the Wscript.Network object and then call the SetDefaultPrinter method in order to make the selected printer the default printer:

Set WshNetwork = CreateObject("Wscript.Network")
WshNetwork.SetDefaultPrinter strPrinter

And then, just for the heck of it, we display a message stating that the selected printer is now the default printer.

And that’s basically all we have to do. Admittedly we don’t have any error-handling here and we don’t have an obvious way for users to close the dialog box without choosing a new default printer. Likewise we could get a little fancier and, say, display the name of the current default printer in a different color. But look at it this way: if we did all the work you might start to feel like no one needed you, and you might be tempted to jump off a bridge or throw yourself on a scrap heap. Which means that all these things we don’t do are actually something we do for your own good.

You’re welcome.

So which world problem will the Scripting Guys tackle next? Well, there’s definitely no shortage of problems in the world. However, we have our sights fixed on the biggest one of all: those commercials aimed at male … dysfunctions …. Although we try not to be prudish, well, have you ever seen some of these commercials? Could you imagine meandering through a bookstore, just hoping to find a book, and instead stumbling upon – well, you know. Or trying to eat a sandwich in a restaurant only to look up and see two people – people old enough to know better – mauling each other? When is a moment the right moment? Trust is here: for some things, never.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • you sir are a life saver.