{"id":65133,"date":"2007-04-06T20:58:00","date_gmt":"2007-04-06T20:58:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/04\/06\/how-can-i-let-users-choose-a-default-printer-from-a-list-of-printers\/"},"modified":"2007-04-06T20:58:00","modified_gmt":"2007-04-06T20:58:00","slug":"how-can-i-let-users-choose-a-default-printer-from-a-list-of-printers","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-let-users-choose-a-default-printer-from-a-list-of-printers\/","title":{"rendered":"How Can I Let Users Choose a Default Printer From a List of Printers?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>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?<BR><BR>&#8212; JD<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, JD. You know, a lot of people write in to say, \u201cHey, Scripting Guys! Why do you limit yourselves to system administration scripting? Don\u2019t you realize that you guys, and you guys alone, have the power to make the world a better place?\u201d <\/P>\n<P>Now, to be honest, sometimes we \u2013 what\u2019s that? How <I>many<\/I> people write in to say that we, and we alone, have the power to make the world a better place? Well, it\u2019s not really our style to keep <I>count<\/I> of that sort of thing. Nevertheless, we\u2019re sure that it\u2019s \u2013 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.<\/P>\n<P>Case in point? Following this year\u2019s Super Bowl we made <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/feb07\/hey0206.mspx\"><B>particular note<\/B><\/A> 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 <I>made<\/I> 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.<\/P>\n<P>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\u2019re going to fire you and, when that happens, you might as well just kill yourself. That\u2019s 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.<\/P>\n<P>Now, we know what you\u2019re thinking: that <I>is<\/I> an odd message to send, isn\u2019t it? But what can one person do about that, even if that one person <I>is<\/I> a Scripting Guy? Well, here\u2019s 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 <I>doesn\u2019t<\/I> stand atop a bridge ready to jump off and commit suicide. That can mean only one thing: the auto maker read our <I>Hey, Scripting Guy!<\/I> column, realized the error of its ways, and changed the commercial.<\/P>\n<P>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.<\/P>\n<P>The moral of this story (and yes, all <I>Hey, Scripting Guy!<\/I> stories have a moral \u2026 somewhere) is that one person <I>can<\/I> make a difference, one person <I>does<\/I> 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:<\/P><PRE class=\"codeSample\">&lt;SCRIPT Language=&#8221;VBScript&#8221;&gt;<\/p>\n<p>    Sub Window_Onload\n        strComputer = &#8220;.&#8221;<\/p>\n<p>        Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\n        Set colPrinters = objWMIService.ExecQuery(&#8220;Select * From Win32_Printer&#8221;)<\/p>\n<p>        For Each objPrinter in colPrinters\n            strPrinter = objPrinter.Name\n            Set objOption = Document.createElement(&#8220;OPTION&#8221;)\n            objOption.Text = strprinter\n            objOption.Value = strPrinter\n            AvailablePrinters.Add(objOption)\n        Next\n    End Sub<\/p>\n<p>    Sub SetDefault\n        strPrinter = AvailablePrinters.Value\n        Set WshNetwork = CreateObject(&#8220;Wscript.Network&#8221;)\n        WshNetwork.SetDefaultPrinter strPrinter\n        Msgbox strprinter &amp; &#8221; has been set as your default printer.&#8221;\n    End Sub<\/p>\n<p>&lt;\/SCRIPT&gt;<\/p>\n<p>&lt;select size=&#8221;5&#8243; name=&#8221;AvailablePrinters&#8221;&gt;&lt;\/select&gt;&lt;p&gt;\n&lt;input type=&#8221;button&#8221; value=&#8221;Set as Default&#8221; onClick=&#8221;SetDefault&#8221;&gt;\n<\/PRE>\n<P>Let\u2019s talk about what we have here; what we <I>do<\/I> 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\u2019s <I>not<\/I> a very good explanation. If you need more information about HTAs, well, that\u2019s what the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/htas.mspx\"><B>HTA Developers Center<\/B><\/A> is for.) VBScript doesn\u2019t have a native method for creating graphical user interfaces. That\u2019s a problem, but we can work around that problem by creating an HTA, and then calling that HTA from within our logon script.<\/P>\n<P>And that\u2019s exactly what we\u2019ve done here: we\u2019ve created a .hta file, a file that we\u2019ll need to copy to each of our computers. (But that\u2019s 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:<\/P><PRE class=\"codeSample\">Set objShell = CreateObject(&#8220;Wscript.Shell&#8221;)\nobjShell.Run &#8220;C:\\Scripts\\Test.hta&#8221;\n<\/PRE>\n<P>So what will people see when this HTA pops up? They\u2019ll see \u2013 in all its barebones splendor \u2013 something that looks like this:<\/P><IMG height=\"247\" alt=\"HTA\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/pickprinter.jpg\" width=\"401\" border=\"0\"> \n<P><BR>Yes, we know. But if you want beautiful you\u2019re on your own.<\/P>\n<P>Now that you understand the basic scenario let\u2019s take a closer look at the HTA itself. As far as the HTML tagging goes, well, there really isn\u2019t much: all we have is a list box named <I>AvailablePrinters<\/I> (added to the HTA by using the <B>&lt;SELECT&gt;<\/B> tag) and a button (<B>&lt;INPUT TYPE = &#8220;BUTTON&#8221;&gt;<\/B>):<\/P><PRE class=\"codeSample\">&lt;select size=&#8221;5&#8243; name=&#8221;AvailablePrinters&#8221;&gt;&lt;\/select&gt;&lt;p&gt;\n&lt;input type=&#8221;button&#8221; value=&#8221;Set as Default&#8221; onClick=&#8221;SetDefault&#8221;&gt;\n<\/PRE>\n<P>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.<\/P>\n<P>That\u2019s right: just like magic.<\/P>\n<P>If you\u2019ve done much HTML tagging you probably noticed that, as configured, our list box is empty; it doesn\u2019t include a single installed printer. Why not? Well remember, that\u2019s 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 <I>that<\/I>? Why, by using this subroutine, of course:<\/P><PRE class=\"codeSample\">Sub Window_Onload\n    strComputer = &#8220;.&#8221;<\/p>\n<p>    Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\n    Set colPrinters = objWMIService.ExecQuery(&#8220;Select * From Win32_Printer&#8221;)<\/p>\n<p>    For Each objPrinter in colPrinters\n        strPrinter = objPrinter.Name\n        Set objOption = Document.createElement(&#8220;OPTION&#8221;)\n        objOption.Text = strPrinter\n        objOption.Value = strPrinter\n        AvailablePrinters.Add(objOption)\n    Next\nEnd Sub\n<\/PRE>\n<P>This, needless to say, is a subroutine named <B>Window_Onload<\/B>; 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:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colPrinters = objWMIService.ExecQuery(&#8220;Select * From Win32_Printer&#8221;)\n<\/PRE>\n<P>That was easy, wasn\u2019t it? The question now is this: what do we <I>do<\/I> with this collection of printers?<\/P>\n<P>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 <B>Name<\/B> property and store it in a variable named strPrinter. We then run headlong into <I>this<\/I> line of code:<\/P><PRE class=\"codeSample\">Set objOption = Document.createElement(&#8220;OPTION&#8221;)\n<\/PRE>\n<P>What we\u2019re doing here is creating an instance of the HTML <B>Option<\/B> object; as the name implies, this object represents an individual option in a list box. After creating a \u201cblank\u201d option we then assign the printer name to the option\u2019s <B>Text<\/B> and <B>Value<\/B> properties:<\/P><PRE class=\"codeSample\">objOption.Text = strPrinter\nobjOption.Value = strPrinter\n<\/PRE>\n<P>In case you\u2019re 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\u2019t 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 <I>Washington<\/I>; that way people would see the name <I>Washington<\/I> in the list box. At the same time, however, you could set the value of that option to <I>WA<\/I>. That way people would see a \u201cfriendly\u201d name in the list box, but you could still use the official postal abbreviation in your script.<\/P>\n<P>After we assign the property values we then call the <B>Add<\/B> method and add the option (that is, the name of the first printer) to the list box:<\/P><PRE class=\"codeSample\">AvailablePrinters.Add(objOption)\n<\/PRE>\n<P>And then we repeat the process with the next printer in the collection.<\/P>\n<P>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 <B>Set as Default<\/B> button. Clicking the button causes the following subroutine to run:<\/P><PRE class=\"codeSample\">Sub SetDefault\n    strPrinter = AvailablePrinters.Value\n    Set WshNetwork = CreateObject(&#8220;Wscript.Network&#8221;)\n    WshNetwork.SetDefaultPrinter strPrinter\n    Msgbox strPrinter &amp; &#8221; has been set as your default printer.&#8221;\nEnd Sub\n<\/PRE>\n<P>As you can see, we\u2019re 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:<\/P><PRE class=\"codeSample\">strPrinter = AvailablePrinters.Value\n<\/PRE>\n<P>We use these two lines of code to create an instance of the <B>Wscript.Network<\/B> object and then call the <B>SetDefaultPrinter<\/B> method in order to make the selected printer the default printer:<\/P><PRE class=\"codeSample\">Set WshNetwork = CreateObject(&#8220;Wscript.Network&#8221;)\nWshNetwork.SetDefaultPrinter strPrinter\n<\/PRE>\n<P>And then, just for the heck of it, we display a message stating that the selected printer is now the default printer.<\/P>\n<P>And that\u2019s basically all we have to do. Admittedly we don\u2019t have any error-handling here and we don\u2019t 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 <I>all<\/I> 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\u2019t do are actually something we do for your own good.<\/P>\n<P>You\u2019re welcome.<\/P>\n<P>So which world problem will the Scripting Guys tackle next? Well, there\u2019s 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 \u2026 dysfunctions \u2026. Although we try not to be prudish, well, have you ever <I>seen<\/I> some of these commercials? Could you imagine meandering through a bookstore, just hoping to find a book, and instead stumbling upon \u2013 well, you know. Or trying to eat a sandwich in a restaurant only to look up and see two people \u2013 people old enough to know better \u2013 mauling each other? When is a moment the right moment? Trust is here: for some things, <I>never<\/I>.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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?&#8212; JD Hey, JD. You know, a lot of people write in to say, \u201cHey, Scripting Guys! Why do you limit yourselves to system administration scripting? Don\u2019t you realize [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[445,404,3,5],"class_list":["post-65133","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-client-side-printing","tag-printing","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>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?&#8212; JD Hey, JD. You know, a lot of people write in to say, \u201cHey, Scripting Guys! Why do you limit yourselves to system administration scripting? Don\u2019t you realize [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=65133"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65133\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=65133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}