Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using HTA to add network printer 1

Status
Not open for further replies.

nkntb

Technical User
Jun 3, 2005
8
US
When I run the script the msgbox returned the corrected printer name but the script error out saying the printer name is invaild.

I think the problem is objButton.Value it is not a string, but how can I convert it to be useful string?

Thank you,

Chi

here is my script.

<html>

<head>

<title>Connect Printer Helper</title>

<HTA:APPLICATION

ID="objTest"
APPLICATIONNAME="Add Printer"
SCROLL="yes"
SINGLEINSTANCE="yes"

>

</head>

<SCRIPT LANGUAGE="VBScript">

Sub AddSub
For Each objButton in RadioOption
If objButton.Checked Then
Msgbox "You selected " & objButton.Value
Set WshNetwork = CreateObject("WScript.Network")
WshNetwork.AddWindowsPrinterConnection Chr(34) & objButton.Value & Chr(34)
WshNetwork.SetDefaultPrinter Chr(34) & objButton.Value & Chr(34)
End If
Next
End Sub

</SCRIPT>

<body>

<input type="radio" name="RadioOption" value="\\printserver\hp1300">Building 12<BR>
<input type="radio" name="RadioOption" value="\\printserver\hp4200">Admin Office<BR>
<input type="radio" name="RadioOption" value="\\printserver\hp4000">It Dept<P>
<input id=runbutton class="button" type="button" value="Add Printer" name="run_button" onClick="AddSub">

</body>
</html>

 
>I think the problem is objButton.Value it is not a string, but how can I convert it to be useful string?
>[tt]For Each objButton in RadioOption[/tt]

[tt]For Each objButton in document.GetElementsByName("RadioOption")[/tt]

Also put those buttons inside a form would be safer to avoid surprise if any.
 
Hi Tsuji,

Thank you for your respond.

I changed the following like you suggested.

> For Each objButton in RadioOption

to For Each objButton in document.GetElementsByName("RadioOption")

I am still getting the printer name is invalid error message.

I even tried to write the objButton.value to a text file and it show the printer name as excepted.

Any other suggestion will be greatly appreciated.

Thank you,

Chi
 
msdoc said:
AddWindowsPrinterConnection Method
Adds a Windows-based printer connection to your computer system.

Windows NT/2000:
object.AddWindowsPrinterConnection(
strPrinterPath
)
Windows 9x/Me:
object.AddWindowsPrinterConnection(
strPrinterPath,
strDriverName[,strPort]
)
Arguments
object
WshNetwork object.
strPrinterPath
String value indicating the path to the printer connection.
strDriverName
String value indicating the name of the driver (ignored if used on Windows NT/Windows 2000).
strPort
Optional. String value specifying a printer port for the printer connection (ignored on Windows NT/Windows 2000).
Remarks
Using this method is similar to using the Printer option on Control Panel to add a printer connection. Unlike the AddPrinterConnection method, this method allows you to create a printer connection without directing it to a specific port, such as LPT1. If the connection fails, an error is thrown. In Windows 9x/Me, the printer driver must already be installed on the machine for the AddWindowsPrinterConnection method to work. If the driver is not installed, Windows returns an error message.
Check if the os is winnt or 9x?
 
The following two methods works very well with .vbs script the error only pop-up when I intergraded into this .hta script. I can change the .hta script to use checkbox instead of button but I really don't want to hard code the StrPrinterPath because we have about 30 network printers on our network.

First mothod:
Set WshNetwork = CreateObject("WScript.Network")
WshNetwork.AddWindowsPrinterConnection "\\printserver\hp4000"
WshNetwork.SetDefaultPrinter "\\printerserver\hp4000"

Second mothod:
Dim var
var = "\\printserver\hp4000"
Set WshNetwork = CreateObject("WScript.Network")
WshNetwork.AddWindowsPrinterConnection Chr(34) & var & Chr(34)
WshNetwork.SetDefaultPrinter Chr(34) & var & Chr(34)



 
>...works very well with .vbs script the error only pop-up when I intergraded into this .hta script

It should only be due to other stuff, other part of the script or the correctness of the path itself. The getting of the printer path should have no problem as such.

>I can change the .hta script to use checkbox instead of button but I really don't want to hard code the StrPrinterPath because we have about 30 network printers on our network.

I don't follow the logic. You can sure use checkbox as well as radio button. It follows similar script only that you can add more than one. You have to assign the same name to the group of checkbox.
 
If I use checkbox the name of the checkbox will be something like checkbox01, checkbox02 checkbox03, etc.
here is what the script look like when I use checkbox.

Thank you,

<Script>

Sub AddPrinterSub
Set Network = CreateObject("Wscript.Network")
If Checkbox01.Checked Then
Network.AddWindowsPrinterConnection "\\printserver\Hp1300"
End If
If Checkbox02.Checked Then
Network.AddWindowsPrinterConnection "\\printserver\HP4050"
End If
If Checkbox03.Checked Then
Network.AddWindowsPrinterConnection "\\printserver\HP4000"
End If
End Sub

</script>

<Body>
<input type="checkbox" name="Checkbox01">Building 12<br>
<input type="checkbox" name="Checkbox02">Admin Office<br>
<input type="checkbox" name="Checkbox03">IT Dept<p>
</Body>
 
I still don't get the message. Is it then still not working for radio buttons? Why suddently all the checkbox things? There is no reason to need hardcode like neither for checkbox or radio button.
 
tsuji,

I got confuse your last suggestion was correct.
I do need to look at the way AddWindowsPrinterConnection Method works.

Thank you for you time,

-C
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top