Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get vbscript to show a friendly error message?

Tags:

vbscript

I'm installing a network printers using vbscript and I want to show a friendly error if the queue doesn't exist or the printer server is unavailable, can I do this with VBScript? My code is below.

Dim net
Set net = CreateObject("WScript.Network") 
net.AddWindowsPrinterConnection "\\printsrv\HPLaser23"
net.SetDefaultPrinter "\\printsrv\HPLaser23"

Many thanks for the help

Steven

like image 681
Steve Wood Avatar asked Nov 05 '22 09:11

Steve Wood


1 Answers

Add the line:

On Error Resume Next ' the script will "ignore" any errors 

Before your code

and then do an:

if  Err.Number <> 0  then 
     ' report error in some way
end if
On Error GoTo 0 ' this will reset the error handling to normal

After your code

It's normally best to try to keep the number of lines of code between the On Error Resume Next and the On Error GoTo 0 to as few as possible, since it's seldom good to ignore errors.

like image 151
Hans Olsson Avatar answered Dec 30 '22 19:12

Hans Olsson