The negation operator can indicate the negative value of a numeric expression. Its usage is
-number
where number is any numeric expression.
NOTE
The syntax of the usage distinguishes between the negation operator and the subtraction operator.
NOT Operator
The NOT operator can perform a logical negation of an expression as well as an algebraic bitwise reversal of an expression. Its usage is
result = NOT expr
where result is a numeric variable and expr is any expression. When used as a negation operator, if expr is True, result will be False. If expr is False, result will be True. If expr is Null, result will be Null.
When used as an algebraic operator, each bit in result will be cleared if set in expr or set if clear in expr.
Comparison Operators
Comparison operators match two expressions and provide alternative program execution flow. Their usage is
result = expr1 op expr2
result = obj1 IS obj2
result = string LIKE pattern
where op is the comparison operator, result is a numeric variable, expr1 and expr2 are expressions, obj1 and obj2 are objects, string is a string variable, and pattern is a string expression.
NOTE
The logical operators can also be used as comparison operators.
The general rules for comparisons are
●If both expressions are numeric, a numeric comparison is performed.
●If both expressions are strings, a string comparison is performed.
●When comparing a string to a numeric, the numeric expression is always the lesser of the two.
●If one expression is Empty and the other is a numeric expression, the Empty expression is treated as a numeric 0. If both are Empty, then they are both treated as numeric 0 and are equal.
●If one expression is Empty and the other is a string expression, the Empty expression is treated as a Null string.
●If either expression is Null, the result is Null.
●If both expressions are Empty, the expressions are equal.
<(Less than) Operator
The less than operator's usage is
expr1 < expr2
and a True result is yielded when expr1 is arithmetically less than expr2.
<= (Less than or equal to) Operator
The less than or equal to operator's usage is
expr1 <= expr2
and a True result is yielded when expr1 is arithmetically less than or equal to expr2.
> (Greater than) Operator
The greater than operator's usage is
expr1 > expr2
and a True result is yielded when expr1 is arithmetically greater than expr2.
>= (Greater than or Equal to) Operator
The greater than or equal to operator's usage is
expr1 >= expr2
and a True result is yielded when expr1 is arithmetically greater than or equal to expr2.
= (Equal to) Operator
The equal to operator's usage is
expr1 = expr2
and a True result is yielded when expr1 is arithmetically equal to expr2.
<> (Not Equal to) Operator
The not equal to operator's usage is
expr1 <> expr2
and a True result is yielded when expr1 is arithmetically not equal to expr2.
Is Operator
The Is operator compares two object reference variables. Its usage is
obj1 Is obj2
where obj1 and obj2 are object names. If obj1 and obj2 both refer to the same object, the condition is True. Two variables can be made to refer to the same object by using the Set statement.
Statements
This section describes the statement syntax for the VBScript language. In the descriptions the use of brackets ([]) indicates an optional entry or keyword.
Call Statement
The Call statement transfers control to a Sub procedure or Function procedure. Its usage is
Call name ([[ByVal] arg1, [ByVal] arg2, ... [ByVal] argn])
[result =] name [[ByVal] arg1, [ByVal] arg2, ... [ByVal] argn]
where name is the name of the procedure to call, arg1 through argn are a comma-delimited list of expressions to pass to the procedure, and result is any numeric variable. If the ByVal keyword precedes a variable in the argument list , the argument is being passed to the procedure by value and the procedure being called may change the value of the variable. In the first syntax, the argument list must be enclosed in parentheses and a result is not available.
Dim Statement
The Dim statement declares variables and allocates storage space. Its usage is
Dim var1[([subs])][, var2[([subs])]] . . .
where var1 and var2 are names of variables to be declared and subs are upper-bound dimensions for an array variable. An array can have up to 60 dimensions. The lower bound of an array is always 0. Scoping rules will apply to the variables declared. When variables are initialized, a numeric variable is initialized to 0 and a string is initialized to a zero-length string ("").
If the subs fields are not specified (empty parentheses), a dynamic array is indicated. In this case the ReDim statement can be used later to define the number of dimensions and elements in the array.
Do...Loop Statement
The Do loop repeats a block of statements while a condition is True or until a condition becomes True. The two possible usages are
Do [{While | Until} condition]
[statements]
Loop
Do
[statements]
Loop [{While | Until} condition]
where condition is an expression that can be evaluated to True or False. Statements are various statements that are repeated while or until condition is True. To exit the loop immediately, use the Exit Do statement.
Erase Statement
The Erase statement will free the storage used by a dynamic array. If the array is fixed size, the elements in the array will be reset. Its usage is
Erase array
where array is the name of the array to be erased.
Exit Statement
The Exit statement is used to escape from a block of code. The statement varies depending on the type of block
involved. Its usages are
Exit Do
Exit For
Exit Function
Exit Sub
For...Next Statement
The For...Next loop repeats a group of statements a specified number of times and optionally varies a variable within the loop. Its usage is
For count = start To end [Step step]
[statements]
Next
where count is a numeric variable used as a loop counter, start is the beginning value for count, end is the ending value, and step is the amount count is to change (defaulting to 1) for each iteration of the loop. Statements is the block of code to be executed on each iteration. The sequence of loop iterations can be either positive or negative, depending upon the step value.
The Exit For statement can be used to escape from the loop.
For Each...Next Statement
The For Each...Next statement is a variation of the For loop that can repeat a block of code for each element in an array. Its usage is
For Each entry In array
[statements]
Next [entry]
where entry is a variable used to iterate through the elements of the array, array is the name of a collection or array, and statements is a block of code to be executed on each iteration. An Exit For can be used to escape from the loop.
Function Statement
The Function statement defines the block of code that makes up a function procedure. It encompasses the name and arguments of that procedure. Its usage is
Function name [[ByVal] arg1, [ByVal] arg2, ... [ByVal] argn]
[statements]
[name = expression]
End Function
where name is the name of the function, arg1 through argn is a list of variables passed to the procedure, and statements is a block of code to be executed. The name = expression optional line returns a value to the caller of the function. The ByVal keyword indicates an argument whose value may be changed during the procedure.
An Exit Function statement can be used to escape from the function at any point.
If...Then Statement
The If...Then statement provides alternative statement execution depending upon varying conditions that may be present. Its usage is
If condition-1 Then
[statements]
[ElseIf condition-2 Then
[elifstatements]]
[Else
[elstatements]]
End If
where condition-1and condition-2are conditional expressions (see comparison and logical operators above), statements is a block of code executed when condition-1is True, elifstatements is a block of code executed when condition-1is False and condition-2is True, and elstatements is a block of code that is executed when neither condition-1nor condition-2is True.
NOTE
A single line form of the If statement is available; however, its use is discouraged for readability reasons.
On Error Statement
The On Error statement identifies an error-handling routine and specifies the location of the routine within a procedure. It can also be used to disable an error-handling routine. Its usage is
On Error Resume Next
Randomize Statement
The Randomize statement sets a new seed value into the random-number generator. Its usage is
Randomize [number]
where number is a numeric expression.
ReDim Statement
The ReDim statement declares dynamic-array variables and allocates or reallocates storage space. Its usage is
ReDim [Preserve] name(subs) [,name(subs)] . . .
where Preserve indicates that the existing values in an array are to be saved when changing the size of the last dimension, name is the name of a variable, and subs are the redimensions of an array variable.
Rem Statement
The Rem statement is a nonexecutable statement and provides documentary remarks in a program. Its usages are
Rem comment
or
' comment
where comment is the text of any remarks you want to include.
Select Case Statement
Select Case executes one of several groups of statements, depending on the value of an expression. Its usage is
Select Case testexpr
[Case expr1
[statements1]]
[Case expr2
[statements2]]
.
.
.
[Case exprn
[statementsn]]
[Case Else
[elstatements]]
End Select
where testexpr is any expression; expr1, expr2, ..., and exprn are alternative values for testexpr; and statements1, statements2, ..., and statementsn are blocks of code that are executed when the value of testexpr matches the respective case expression. elstatements is a block of code that is executed when testexpr doesn't match any of the case expressions.
Set Statement
The Set statement assigns an object reference to a variable or property. Its usage is
Set objectvar = { objectexpr | Nothing}
where objectvar is the name of a variable and objectexpr is the name of an object, a variable of an object type, or a function that returns an object of an object type. If Nothing is specified, the name of the object is disassociated from objectvar.
Sub Statement
The Sub statement defines the block of code that makes up a subroutine. It encompasses the name and arguments of that routine. Its usage is
Sub name [[ByVal] arg1, [ByVal] arg2, ... [ByVal] argn]
[statements]
End Sub
where name is the name of the subroutine, arg1 through argn is a list of variables passed to the procedure, and statements is a block of code to be executed. Unlike a procedure, a subroutine cannot be used on the right side of a statement and does not return a value. The ByVal keyword indicates an argument whose value may be changed during
the procedure.
An Exit Sub statement can be used to escape from the function at any point.
While...Wend Statement
The While statement is similar to the Do While statement and executes a block of code while a condition is True. Its usage is
While condition
[statements]
Wend
where condition is a comparison or logical expression that can be evaluated to True or False and statements is the block of code to be executed.
Functions
The VBScript language offers a number of built-in procedures that are used to extend the functionality of the language. These are implemented as functions and as such will return a value that can be used in expressions. For convenience these functions can be grouped by purpose.
Variable and Conversion Functions
The variable and conversion functions deal directly with the types of variables and offer ways to convert these variables from one type to another. Refer to Table A.1 for more information.
CBool Function
The CBool function returns a Boolean value that depends on the value of the argument. Its usage is
result = CBool(expr)
where result is an expression that is a Variant of subtype Boolean and expr is a valid expression that can be evaluated to a numeric value. If expr is 0, the function returns False; otherwise, it returns True. If expr cannot be evaluated to a numeric value, the function causes a runtime error.
CByte Function
The CByte function converts an expression into a byte value. Its usage is
result = CByte(expr)
where result is a Variant of subtype Byte and expr is a valid expression with a value in the byte range. If expr is not in the byte range, an error occurs.
CDbl Function
The CDbl function returns an expression that has been converted to a Variant of subtype Double. Its usage is
result = CDbl(expr)
where result is a Variant of subtype Double and expr is a valid expression with a value in the double range.
Chr Function
The Chr function converts an ANSI character code into a character. Its usage is
result = Chr(charcode)
where result is a character and charcode is a number that identifies an ANSI character.
NOTE
Another function (ChrB) is provided for use with byte data contained in a string. Instead of returning a character, which may be one or two bytes, ChrB always returns a single byte. ChrW is provided for 32-bit platforms that use Unicode characters. Its argument is a Unicode (wide) character code, thereby avoiding the conversion from ANSI to Unicode.
ChrB Function
The ChrB function converts an ANSI character code into a single byte. Its usage is
result = ChrB(charcode)
where result is a byte subtype and charcode is a number that identifies an ANSI character.
ChrW Function
The ChrW function converts an ANSI character code into a Unicode character. Its usage is