Method to terminate script?
Moderators: Gurus, Addon Administrators
Method to terminate script?
Is there a method call that will exit my script?
Like VBscript EXIT SUB but exiting the script rather than just the sub.
Like VBscript EXIT SUB but exiting the script rather than just the sub.
Chris
Re: Method to terminate script?
There's the STOP statement but it only works in WScript, not VBscript.
I think the only way to do it is to voluntarily generate an erroror
It's convenient for debugging but not suitable for public release...
I think the only way to do it is to voluntarily generate an error
Code: Select all
Err.Raise <Number>
Code: Select all
x = 10 / 0
Re: Method to terminate script?
I can't find a way to abrubtly quit, but perhaps you could use a global variable that is set to True when you want to quit. Then have your other subroutines check that variable at key intervals and if it is set to True then Exit Sub.
Re: Method to terminate script?
Thanks. Trying it http://i.imgur.com/9o87Iw9.png (there's no <number> for script-special error) I find generates two dialog boxes, so is not as convenient as I'd like.Eyal wrote:There's the STOP statement but it only works in WScript, not VBscript.
I think the only way to do it is to voluntarily generate an errororCode: Select all
Err.Raise <Number>
It's convenient for debugging but not suitable for public release...Code: Select all
x = 10 / 0
Chris
Re: Method to terminate script?
That's the litter I already have and am seeking to eliminate.rivorson wrote:I can't find a way to abrubtly quit, but perhaps you could use a global variable that is set to True when you want to quit. Then have your other subroutines check that variable at key intervals and if it is set to True then Exit Sub.
Thanks anyway for the suggestion.
Chris
Re: Method to terminate script?
I have found a work around that uses Eyal's method with error handling to avoid showing the error boxes. Just add an extra sub that runs before your main sub like so:
Have AllowEarlyExit as the trigger and when MainSub causes an error it will just skip back to AllowEarlyExit and continue on to the MsgBox without actually showing the error.
Code: Select all
Sub AllowEarlyExit()
On Error Resume Next
MainSub
MsgBox "MainSub finished"
End Sub
Sub MainSub
'some code
Err.Raise 1 'throws error
'some more code
End Sub
Re: Method to terminate script?
That will hide all the genuine error boxes too.rivorson wrote:I have found a work around that uses Eyal's method with error handling to avoid showing the error boxes. Just add an extra sub that runs before your main sub like so:
...
Have AllowEarlyExit as the trigger and when MainSub causes an error it will just skip back to AllowEarlyExit and continue on to the MsgBox without actually showing the error.
Is there some way to make the hiding specific to the user error? I can find no Error.Raise number for use error but perhaps it would be safe enough to repurpose an existing rare error number.
Chris
Re: Method to terminate script?
Error numbers can be between 0 and 65535. 0 to 512 are reserved for errors defined by VB. 513-65535 are available for user defined errors, presumably including errors defined by MM. I suggest raising the highest error number possible to terminate your code then check if there was an error that is smaller than that.
Code: Select all
Sub AllowEarlyExit()
On Error Resume Next
Dim ErrorNumber as Integer
MainSub
ErrorNumber = Err.Number
If ErrorNumber > 0 And ErrorNumber < 65535 Then Err.Raise ErrorNumber
MsgBox "MainSub finished"
End Sub
Sub MainSub
'some code
Err.Raise 65535 'throws error
'some more code
End Sub
Re: Method to terminate script?
If you don't already have VBScript Language Reference (VBSCRIP5.CHM), you can find its list of errors here:
https://msdn.microsoft.com/en-us/librar ... s.84).aspx
and On Error handling is explained a bit here: https://msdn.microsoft.com/en-us/librar ... s.84).aspx
https://msdn.microsoft.com/en-us/librar ... s.84).aspx
and On Error handling is explained a bit here: https://msdn.microsoft.com/en-us/librar ... s.84).aspx