Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the code to exit/ stop VBscript from running in the event of a condition not being met?

Tags:

vbscript

I have looked on Google and the answer is not there!

First things first. WScript.Quit DOES NOT WORK! I have no idea what "WScript" is but it clearly has nothing to do with client side scripting for a web page. I have seen this "WScript" thing somewhere before and it just produces errors (maybe obsolete or something) so please do not suggest it...

Anyway... all I wish to do is completely stop the script in the event of a condition not being met. Obviously I don't want "Exit Sub" because the code would then carry on running if that sub is embedded!

I am aware of the "stop" command but I am under the impression that it is only used for debugging.

Hopefully a very simple question.

UPDATE and Conclusion: Before I close this subject I will just expand a little on what I was trying to do...

I had a number of main subs that were being started by a button click. In order to make it so that I did not have to edit each individual sub I embedded a universal sub within each one that did a preliminary check.

Part of that preliminary check was to stop the program in the case of an incorrect user input. If an error was detected I wanted to halt all progress from that point on. An "exit sub" would obviously just skip the rest of that preliminary sub and the main sub would carry on executing.

In the end it was just a case of writing in an error flag (that is checked in the main subs) or incorporating the error condition operation in each main procedure. In that way you exit the main sub and the problem is solved.

It was not laziness - I just wanted to reduce the amount of code. Thank you for the responses anyway.

like image 221
Cheesus Toast Avatar asked Nov 21 '12 23:11

Cheesus Toast


People also ask

How do you exit a VBScript code?

A Exit For Statement is used when we want to Exit the For Loop based on certain criteria. When Exit For is executed, the control jumps to next statement immediately after the For Loop.

How do you exit a while loop in VBScript?

An Exit Do Statement is used when we want to Exit the Do Loops based on certain criteria. It can be used within both Do.. While and Do..

What is wscript quit?

Quit is a wscript method. Examples WScript.Quit 1. If calling a VBScript from a batch file, catch the Errorlevel with an IF statement. cscript.exe MyScript.vbs. IF errorlevel 1 goto s_next.


1 Answers

I've found that the WScript is always available if running a .vbs/.vbe/.wsf script using either the wscript.exe or cscript.exe engine. When WScript is not available is if running using a different engine. E.g. running VBScript within a HTA, a webpage, in VBA or from a hosted COM script control.

To exit a script which is not running from wscript.exe or cscript.exe, you can do something like the following:

main

Sub main
    ' execute code here

    ' oops a confition is not met:
    If Not condition then Exit Sub

    ' more code to execute if condition was met
End Sub
like image 84
sj1900 Avatar answered Sep 30 '22 19:09

sj1900