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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hta error handling

Status
Not open for further replies.

chouck

MIS
Jan 30, 2005
32
US
Hi All,

I have a bit of code that I am having a problem w/. I have a little hta that will let a user select their location, then type in the printer name, and then have some vb code run to add the printer connection. I have it set to where if the user just clicks of without typing anything it will error, but if they type in the name of a non-existant printer it just gives a runtime error and i don't know how to make it so it will display the error i want them to see.

thanks in advance


<HEAD>
<TITLE>Set Up Printer By Location</TITLE>
<HTA:APPLICATION ID="oMyApp"
APPLICATIONNAME="Set Up Printer By Location"
BORDER="Dialog"
CAPTION="Yes"
SCROLL="NO"
SHOWINTASKBAR="yes"
SINGLEINSTANCE="yes"
SYSMENU="Yes"
WINDOWSTATE="normal">
</HEAD>
<BODY>
<SCRIPT LANGUAGE="VBScript">

option explicit
on error resume next

dim pc, net, netprint, wmisvc, printobj, printadd, printcol, printer, printset
dim loc, printsrv, print, printname, ements

pc = "."
set net = createobject("wscript.network")

Sub Window_OnLoad
Window.Site.Focus
End Sub

Sub btn01_OnClick
ements = Window.Site.SelectedIndex
loc = Window.Site.Options(ements).Text
print = BasicTextBox.Value

Select Case loc
Case "location 1"


Case "location 2"

Case "location 3"

Case "location 4"
printsrv = "\\location4_server\"
print = BasicTextBox.Value
printset = printsrv & print
msgbox printset
if print = "" then
msgbox "You have not typed in a correct printer name" &_
vbcrlf &_
vbcrlf &_
"please try again",64, "Printer setup error"
window.close
else
net.AddwindowsPrinterConnection (printset)
if err.number <> 0 then
msgbox "You have not typed in a correct printer name" &_
vbcrlf &_
vbcrlf &_
"please try again",64, "Printer setup error"
window.close
end if
end if
Case "location 5"

Case "location 6"


End Select

End Sub

Sub btn02_OnClick
Window.Close
End Sub



</SCRIPT>

<H2>Set Up Printer By Location</H2>
<P>Please select the Site:
<SELECT NAME="site">
<OPTION>location 1</OPTION>
<OPTION>location 2</OPTION>
<OPTION>location 3</OPTION>
<OPTION>location 4</OPTION>
<OPTION>location 5</OPTION>
<OPTION>location 6</OPTION>
</SELECT><P>
<P>Enter Printer Name:
<input type="text" name="BasicTextBox" size="50"><P>

<BR>
<BR>
<Input Type = "Button" Name = "btn01" VALUE = "Set Up Printer">
<Input Type = "Button" Name = "btn02" VALUE = "CLOSE">
<BR>
<BR>

</BODY>
 
Try adding "On Error Resume Next" inside your sub right before the area you could expect an error message...perhaps right before the select case.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Thanks DM that worked like a charm. Now I think I am getting greedy. Is there a way to query the local print server and havve it populate a dropdown list depending on the "case" selected ? I tried using wmi to enumerate the printers but it seems you have t ohave admin rights on the print server to run the query??? Thanks again
 
That's correct...to query with WMI requires admin rights on the server. Just a thought, but perhaps you can try using "net view". i.e. (you may have to play with it a bit)

Code:
Option Explicit

Dim strPrnSvrName : strPrnSvrName = "printserver"
Dim objShell : Set objShell = CreateObject("WScript.Shell")
Dim objExec : Set objExec = objShell.Exec("net view \\" & strPrnSvrName)
Dim strTemp
Do Until objExec.StdOut.AtEndOfStream
	strTemp = objExec.StdOut.ReadLine
	If InStr(strTemp, "Print") > 0 Then 
		WScript.Echo Left(strTemp, InStr(strTemp, " ") - 1)
	End If
Loop

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top