I'm looking to modify this script so I can offer printer support on remote
computers.
<html>
<title>Add Printers</title>
<HTA:APPLICATION
ID="objAddPrinteromatic"
APPLICATIONNAME="AddPrinteromatic"
SCROLL="no"
SINGLEINSTANCE="no"
WINDOWSTATE="normal"
MAXIMIZEBUTTON="no"
MINIMIZEBUTTON="no"
SELECTION="yes"
<head>
<style>
BODY
{
background-color: buttonface;
font-family: Helvetica;
font-size: 8pt;
margin-top: 2px;
margin-left: 8px;
margin-right: 3px;
margin-bottom: 3px;
filter

rogid

XImageTransform.Microsoft.Gradient(GradientType=1,
StartColorStr='#F0F0F0', EndColorStr='#FFFF00')
}
..button
{
font-family: Helvetica;
font-size: 8pt;
width: 50px;
}
..wmibutton
{
font-family: Helvetica;
font-size: 8pt;
width: 70px;
}
..bigger_button
{
font-family: Helvetica;
font-size: 8pt;
width: 110px;
}
textarea
{
font-family: Arial;
font-size: 8pt;
margin-left: 3px;
margin-right: 3px;
}
select
{
font-family: Arial;
font-size: 8pt;
margin-left: 0px;
}
</style>
'********************************************************************
'* BEGIN_SCRIPT
'********************************************************************
<script language="vbscript">
'************************
'* Global State Variables
'************************
'********************************************************************
'* g_strPrintServerList
'* This is the print server we'll check for the search
'* string.
'*
'* The name need to be the NETBIOS names that the end users can
'* connect to without the leading \\'s.
'********************************************************************
g_strPrintServerList = "printserver"
'********************************************************************
'* g_strPrinterFilter
'* This is the filter that we will use to search the print
'* server. Anything that matches this string is returned.
'********************************************************************
g_strPrinterFilter = "filter"
'********************************************************************
'* Window_Onload
'* This is the first script that runs when the window loads.
'* We set the focus on it, resize it to fit our needs, and then
'* start adding the printers based on the Global variables
'* defined above.
'********************************************************************
Sub Window_Onload
self.Focus()
self.ResizeTo 520,275
AddPrinterButton.Disabled = True
RemovePrinterButton.Disabled = True
DefaultPrinterButton.Disabled = True
PrintTestButton.Disabled = True
add_printers.InnerHTML = "<font color=""darkred"" style=""font-size:10pt;
font-weight:Bold;"">Waiting for Printer List...<" & "/font>"
PleaseWait.style.visibility = "hidden"
self.setTimeout "BuildPrinterList", .1
End Sub
'********************************************************************
'* BuildPrinterList
'*
'* This is the script that builds the select boxes for current
'* network printers and the printers that are on the print server.
'********************************************************************
sub BuildPrinterList
GetPrinters
strHTML = "<select style=""width:310px;"" id=""AddPrintersSelect""
name=""AddPrintersSelect"" MULTIPLE>"
strHTML = strHTML & "<option value=""""" & ">" & "Select a Network Printer"
& "<" & "/option>"
PrinterServers = Split(g_strPrintServerList, ",")
strPrintersToAdd = ""
for each PrintServer in PrinterServers
Set objComputer = GetObject("WinNT://" & PrintServer)
For Each objPrinter in objComputer
If Instr(objPrinter.Name, g_strPrinterFilter) Then
strPrintersToAdd = "<option value=" & Chr(34) & "\\" & PrintServer & "\" &
objPrinter.Name & Chr(34) & ">" & objPrinter.Name & "<" & "/option>"&
strPrintersToAdd
End If
Next
Next
strHTML = strHTML & strPrintersToAdd & "</select>"
add_printers.InnerHTML = strHTML
sortlist("AddPrintersSelect")
AddPrinterButton.Disabled = False
end Sub
'********************************************************************
'* QuitScript
'*
'* When the user presses the Quit button, the file where we've
'* been storing the scripts gets deleted and the main window
'* closes.
'********************************************************************
Sub QuitScript
self.Close()
End Sub
'********************************************************************
'* GetPrinters
'*
'* This adds the network printers already connected to the
'* workstation to the Current Network Printers select box. Once
'* it has completed it enables the buttons that pertain to it.
'********************************************************************
Sub GetPrinters
strComputer = "."
Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery _
("Select * From Win32_Printer Where Network = TRUE")
strPrintersLoaded = ""
do while Printers.Length > 0
Printers.Remove(0)
loop
For Each objPrinter in colPrinters
Set objOption = Document.createElement("OPTION")
objOption.Value = objPrinter.Name
if objPrinter.ShareName <> "" then
objOption.Text = objPrinter.ShareName
else
objOption.Text = objPrinter.Name
end if
if objPrinter.Default = True then
objOption.Text = objOption.Text & " Default"
objOption.Selected = True
end if
Printers.Add(objOption)
Next
RemovePrinterButton.Disabled = False
DefaultPrinterButton.Disabled = False
PrintTestButton.Disabled = False
end Sub
'********************************************************************
'* ShowWaitScript
'*
'* When the user presses any button, we check the variable
'* to see we display the please wait block and then run the
'* appropriate script.
'********************************************************************
Sub ShowWaitScript(ActionToPerform)
PleaseWait.style.visibility = "visible"
Select Case ActionToPerform
CASE "Add"
self.setTimeout "RunScript", .1
CASE "Remove"
self.setTimeout "RemoveScript", .1
CASE "Default"
self.setTimeout "DefaultPrinterScript", .1
CASE "Test"
self.setTimeout "PrintTestScript", .1
CASE Else
PleaseWait.style.Visibility = "hidden"
end select
End Sub
'********************************************************************
'* RunScript
'*
'* When the user presses the Add Printer button, we check the
'* textbox to see what Printer is being added and add that printer
'********************************************************************
Sub RunScript
if AddPrintersSelect.SelectedIndex = 0 then
msgbox "You have to select a printer from list before one can be added.",
48, "Select Printer"
PleaseWait.style.visibility = "hidden"
exit sub
end if
strPrinterToAdd = AddPrintersSelect.Value
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection strPrinterToAdd
strPrinterToAdd = AddPrintersSelect.Value & "_mono_simplex$"
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection strPrinterToAdd
On Error Resume Next
strPrinterToAdd = AddPrintersSelect.Value & "_colour_simplex$"
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection strPrinterToAdd
On Error Resume Next
strPrinterToAdd = AddPrintersSelect.Value & "_colour_duplex$"
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection strPrinterToAdd
GetPrinters
AddPrintersSelect.SelectedIndex = 0
PleaseWait.style.visibility = "hidden"
End Sub
'********************************************************************
'* RemoveScript
'*
'* When the user presses the Remove Printer(s) button, we check
'* the select box to see what Printer(s) is being removed and
'* remove it.
'********************************************************************
Sub RemoveScript
if Printers.SelectedIndex = -1 then
msgBox "You have to select a printer from list before one can be removed.",
48, "Select Printer"
PleaseWait.style.visibility = "hidden"
exit sub
end if
i = 0
do while i < Printers.Length
if Printers.Item(i).Selected then
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objPrinters = objWMIService.ExecQuery("Select * from Win32_Printer Where
Name = '" & Replace(Printers.Item(i).Value , "\", "\\") & "'")
For Each objPrinter in objPrinters
objPrinter.Delete_
Next
end if
i = i + 1
loop
GetPrinters
PleaseWait.style.visibility = "hidden"
End Sub
'********************************************************************
'* DefaultPrinterScript
'*
'* When the user presses the Set as Default button, we check the
'* select box to see what Printer(s) is being set and make it the
'* default. Then reorganize the list.
'********************************************************************
sub DefaultPrinterScript
If Printers.SelectedIndex = -1 then
msgbox "You have to select a printer to make default before one can be
set.", 48, "Select Printer"
PleaseWait.style.visibility = "hidden"
exit sub
end if
Set objNetwork = CreateObject("WScript.Network")
objNetwork.SetDefaultPrinter Printers.Item(Printers.SelectedIndex).Value
GetPrinters
PleaseWait.style.visibility = "hidden"
end sub
'********************************************************************
'* PrintTestScript
'*
'* When the user presses the Print Test Page, we check the
'* select box to see what Printer(s) is being tested and then
'* send a test page to it. This does not do multiple tests.
'********************************************************************
sub PrintTestScript
if Printers.SelectedIndex = -1 then
msgBox "You have to select a printer from list before a test page can be
sent.", 48, "Select Printer"
PleaseWait.style.visibility = "hidden"
exit sub
end if
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery("Select * from Win32_Printer Where
DeviceID = '" & Replace(Printers.Item(Printers.SelectedIndex).Value, "\",
"\\") & "'")
For Each objPrinter in colPrinters
errReturn = objPrinter.PrintTestPage
If errReturn = 0 Then
msgbox "The test page was printed successfully to" &
Printers.Item(Printers.SelectedIndex).Value & "."
Else
msgbox "The test page could not be printed to" &
Printers.Item(Printers.SelectedIndex).Value & "."
End If
Next
PleaseWait.style.visibility = "hidden"
end sub
</script>
<script language="JavaScript" type="text/javascript">
/*********************************************************************
* sortlist(ddlPrinters)
*
* This utilizes the built in Array.sort function of javascript
* to sort the list of printers so they will be displayed in an
* ascending order. Otherwise the printers are filled in wherever
* and difficult to find. The ddlPrinters variable must be a
* selectbox.
*********************************************************************/
function sortList(ddlPrinters) {
var selectBox = document.getElementById(ddlPrinters);
selectArray = new Array();
if(selectBox.length < 2)
{
return;
}
for (i = 1; i < selectBox.length; i++) {
selectArray = new Array();
selectArray[0] = selectBox.options.text;
selectArray[1] = selectBox.options.value;
}
selectArray.sort();
for (j = 0; j < selectBox.length - 1; j++) {
selectBox.options[j+1].text = selectArray[j][0];
selectBox.options[j+1].value = selectArray[j][1];
}
}
</script>
</head>
<body>
<table WIDTH="400" BORDER=1>
<tr>
<td VALIGN=TOP WIDTH=700 COLSPAN=2>
<table BORDER=0>
<tr>
<td colspan=3>
<div style="font-size:8pt;">Add Network Printers</div>
</td>
</tr>
<tr>
<td WIDTH=300 VALIGN=TOP>
<span id="add_printers"></span>
</td>
<td>
<input id=AddPrinterbutton class="bigger_button" type="button" value="Add
Printer" style="font-weight:bold;" name="add_printer_button"
onClick="ShowWaitScript('Add')">
</td>
<td>
<input id=quitbutton class="button" style="background-color: DarkRed;
color:White; font-weight:bold;" type="button" value="Quit" name="quit_button"
onClick="QuitScript()">
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td WIDTH=700>
<table BORDER=0>
<tr>
<td colspan=3>
<div ID=header style="font-size:8pt;">Network Printers Connected</div>
</td>
</tr>
<tr>
<td>
<select id="Printers" name="Printers" multiple="multiple" size="10">
</select>
</td>
<td valign="top">
<input id=RemovePrinterbutton class="bigger_button" type="button"
value="Remove Printer(s)" style="font-weight:bold;"
name="remove_printer_button" onClick="ShowWaitScript('Remove')">
<br />
<input id=DefaultPrinterbutton class="bigger_button" type="button"
value="Set as Default" style="font-weight:bold;"
name="set_default_printer_button" onClick="ShowWaitScript('Default')">
<br />
<input id=PrintTestbutton class="bigger_button" type="button" value="Print
Test Page" style="font-weight:bold;" name="print_test_button"
onClick="ShowWaitScript('Test')">
</td>
<td valign="Top" align="Center">
<span id="PleaseWait" name="PleaseWait" style="font-size:12pt; font-weight:
Bold; color: LightGreen; visibility: hidden;">Please wait</span>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>