Link to home
Start Free TrialLog in
Avatar of MDauphinais1
MDauphinais1

asked on

Quit VBScript On Error

How can I quit the vbscript on error instead of resume next?

I've tried a few variations and nothing works.

I tried:
On Error GoTo Quit

Function Quit()
WScript.Quit
End Function


On Error GoTo 5

5 WScript.Quit
ASKER CERTIFIED SOLUTION
Avatar of itdrms
itdrms
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RobSampson
itdrms is right.
Omitting any On Error Resume Statement will stop VBScript continuing, and upon any error, it will crash out with the error message.

The only way to do it in a user friendly manner is to use the code itdrms provided, at a place where you expect an error may occur.

Regards,

Rob.
Avatar of MDauphinais1
MDauphinais1

ASKER

Hmm..... ok.  Thanks for the help.
I know this is 9 years since this post was originally posted and answered but I'm just starting to use VBScript to automate and simplify specific tasks for some COBOL programs I currently use at home.  I used to use DOS batch files, but do to some recent changes, I've needed the benefits of VBScript.

I have the same situation, I want a script to exit no matter where it is if I've encountered an error.  I use the WScript.Quit object.method.

Here's my sample code;

sub Initialize_Variables()

    sVBScript_Argument=""
    if wscript.arguments.count <> 1 then
        Msgbox "This script must be run with a single argument." & vbcrlf & _
               vbcrlf & "Valid arguments are;" & vbcrlf & _
               "    MOVE_CSV_FILE | RENAME_TO_YYYYMM", _
               vbok, "SSBHL_File_Maint.vbs Run Error!"
        wscript.quit
    end if

    sVBScript_Argument = ucase(wscript.arguments.item (0))

end sub

This subroutine is called from the main routine just after I've dimensioned all my variables.  This script should be run with an argument (MOVE_CSV_FILE or RENAME_TO_YYYYMM).  If there's no argument or more than 1, the messagebox appears and the script exits immediately displaying only my user friendly message box.  I have no ON ERROR RESUME NEXT statement.  I'm not using the err object.  I've found that the Exit Sub statement  is causing some syntax errors.