?
Solved

Script to set default printer

Posted on 2009-04-30
3
Medium Priority
?
759 Views
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
            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">





When I run it to test I get this:



printerror.JPG
0
Comment
Question by:mhmservices
  • 2
3 Comments
 
LVL 5

Expert Comment

by:synx
ID: 24274058
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?
0
 

Author Comment

by:mhmservices
ID: 24278100
http://www.microsoft.com/technet/scriptcenter/resources/qanda/apr07/hey0406.mspx


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.
0
 
LVL 5

Accepted Solution

by:
synx earned 1500 total points
ID: 24282245
I was looking at this one earlier:
http://www.computerperformance.co.uk/ezine/ezine17.htm

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

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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…
Suggested Courses
Course of the Month17 days, 1 hour left to enroll

862 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