Script to set default printer

Posted on 2009-04-30
Last Modified: 2012-05-06
I'd like to create a script or .hta to allow my users to easily change their default printer. So far I have this below.

<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
    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


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

When I run it to test I get this:

Question by:mhmservices
    LVL 5

    Expert Comment

    Does strPrinter contain the server name as well as the printer name?  I know that SetDefaultPrinter is going to want "\\servername\printername".

    Do you know the exact statement that is failing?  If not... Can you throw some message boxes in there to help determine where exactly in the script it is failing?

    Author Comment


    I'm trying to follow the example above. The thing you have to remember is I'm not versed in scripting so I'm finding it difficult to follow.
    LVL 5

    Accepted Solution

    I was looking at this one earlier:

    One thing that's nice (in a way) is that you're actually having a line fail.  So if we can just see which line is bombing we should be able to troubleshoot it.

    What I would do since your script is pretty small would be to add a few message boxes in your code so you can see the last one to pop up and what the value of strPrinter is.  Then when it does fail, you can see what code comes after the last message box that you saw when you ran the script.  So intersperse some lines like:  Msgbox("Position 1"), Msgbox("Position 2"), etc. in your code.  If you're failing when you hit the Submit button, it's the SetDefault function we should look at.  See the attached code for what I'd suggest you test with:
        Sub SetDefault
            strPrinter = AvailablePrinters.Value
            Msgbox "The value of strPrinter is: " & strPrinter
            Set WshNetwork = CreateObject("Wscript.Network")
            Msgbox "WshNetwork created"
            WshNetwork.SetDefaultPrinter strPrinter
            Msgbox strprinter & " has been set as your default printer."
        End Sub

    Open in new window


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    This article was inspired by a question here at Experts Exchange ( The requirements stated in that question are (1) reduce the file size of a large number of…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now