by David Blankenbeckler
This appendix is a summary and quick reference for the VBScript language.
Language keywords and symbols are shown in a monospaced font. Arguments and other parts to be substituted are in italic monospace.
Optional parts are indicated by brackets. If there are several options that are mutually exclusive, they are shown separated by pipes (|) like this:
[ public | private | protected ] type varname
The following words are defined as part of the VBScript language
and cannot be used as variable names:
| Abs | Erase | Len | Set |
| And | Err | Log | Sgn |
| Asc | Error | Loop | Sin |
| Atn | Exit | Ltrim | Sqr |
| Call | Exp | Mid | Step |
| Case | Fix | Minute | Str |
| Cbool | For | Mod | StrComp |
| Cbyte | Function | Month | String |
| Cdate | Hex | MsgBox | Sub |
| CDbl | Hour | Next | Tan |
| Chr | If | Not | Then |
| Cint | Imp | Now | Time |
| Clear | InputBox | Oct | TimeSerial |
| CLng | InStr | On | TimeValue |
| Cos | Int | Or | Trim |
| CSng | Is | Preserve | UBound |
| CStr | IsArray | Raise | UCase |
| Date | IsDate | Randomize | Until |
| DateSerial | IsEmpty | ReDim | Val |
| DateValue | IsNull | Rem | VarType |
| Day | IsNumeric | Right | Weekday |
| Dim | IsObject | Rnd | Wend |
| Do | Lbound | RTrim | While |
| Else | Lcase | Second | Xor |
| Eqv | Left | Select | Year |
Literals in VBScript are represented as follows:
| Syntax | Description |
| number | Integer (base 10) |
| num1.num2 | Floating point |
| num1Enum2 | Exponential floating point |
| "characters" | String |
| #m/d/y# | Date |
| #m-d-y# | Date |
| #hour:min:sec# | Time |
| #m-d-y hour:min:sec# | Date and Time |
| #hour:min:sec m/d/y# | Date and Time |
| True | Boolean |
| False | Boolean |
The VBScript operator is grouped into the following categories: assignment, comparison, arithmetic, string, logical, and bitwise.
The following assignment operator is supported in VBScript:
| Syntax | Description |
| variable = value | Assignment of value to variable |
The following comparison operators are supported in VBScript:
| Description | |
| Equal | |
| Not equal | |
| Greater than | |
| Greater than or equal to | |
| Less than | |
| Less than or equal to | |
| Equal (objects) |
The following arithmetic operators are supported in VBScript:
| Syntax | Description |
| var1 + var2 | Addition |
| var1 - var2 | Subtraction |
| var1 * var2 | Multiplication |
| var1 / var2 | Division |
| var1 & var2 | Modulus |
| var1 \ var2 | Integer division |
| - | Negation |
| var1^exp | Exponentiation |
The following string operator is supported in VBScript:
| Syntax | Description |
| string1 & string2 | String concatenation |
The following logical operators are supported in VBScript:
| Syntax | Description |
| expr1 And expr2 | Logical AND |
| expr1 Or expr2 | Logical OR |
| Not expr | Logical NOT |
| expr1 Xor expr2 | Logical exclusive OR |
| expr1 Eqv expr2 | Equivalence |
| expr1 Imp expr2 | Implication |
The following bitwise logical operators are supported in VBScript:
| Syntax | Description |
| arg1 And arg2 | Bitwise AND |
| arg1 Or arg2 | Bitwise OR |
| arg1 Xor arg2 | Bitwise XOR |
| Not arg1 | Bitwise Not |
| expr1 Eqv expr2 | Bitwise equivalence |
| expr1 Imp expr2 | Bitwise implication |
VBScript statements are divided into two categories: conditional and looping.
Conditional statements provide the ability to perform steps based on the outcome of a comparison. VBScript provides this support through the If...Else and Select Case statements.
The syntax for the If...Else statement is as follows:
If condition Then ' statements for true condition [Else] ' statements for false condition End If
The syntax for the Select Case statement is as follows:
Select Case expr
Case n
' statements for this case
Case m
' statements for this case
'... additional case statements as necessary
[Case Else]
' statements for the default case
End Select
Loop statements provide a means for looping through a section of code until an expression evaluates to true (or false).
The syntax for the For...Next statement is as follows:
For var = init To final [Step step] ' statements to execute while looping Next
The syntax for the While...Wend statement is as follows:
While condition ' statements to execute while looping Wend
The syntax for the Do While statement is as follows:
Do While condition ' statements to execute while looping [Exit Do] ' statements to execute while looping Loop
The syntax for the Do Until statement is as follows:
Do Until condition ' statements to execute while looping [Exit Do] ' statements to execute while looping Loop
The Do Until and Do While loops can also be used with the following syntax:
Do ' statements to execute while looping Loop [Until|While]
VBScript provides two different types of procedures, Sub and Function. When there is no return value, Sub should be used. When there is a return value, Function should be used.
The syntax for a Sub procedure is as follows:
Sub SubName([param1][,param2]...[,paramN]) ' sub procedure statements End Sub
The syntax for a Function procedure is as follows:
Function fnName([param1][,param2]...[,paramN]) ' function statements; fnName = expr End Function
VBScript contains a number of built-in functions. Each of the VBScript functions is summarized in the following sections.
VBScript provides many different built-in functions for performing mathematical operations.
In the following function declarations, numExpr can be either a number or an expression that evaluates to a number.
Abs(numExpr)
Returns the absolute value of numExpr.
Atn(numExpr)
Returns the arc tangent of numExpr.
Cos(numExpr)
Returns the cosine of numExpr.
Exp(numExpr)
Returns e numExpr where e is Euler's constant.
Fix(numExpr)
Returns the integer portion of numExpr. If the number is negative, the next greater integer is returned.
Int(numExpr)
Returns the integer portion of numExpr. If the number is negative, the next lower integer is returned.
Log(numExpr)
Returns the natural log of numExpr.
Rnd([numExpr])
Returns a pseudo-random number. The number is not really random because the same seed will always produce the same result. If numExpr is used, the result is as follows:
If numExpr = 0, Rnd returns the last random number generated.
If numExpr > 0, Rnd returns the next random number in the sequence.
If numExpr < 0, Rnd returns a random number based on the seed, numExpr. The same seed always returns the same number.
The Randomize statement will generate a seed for the Rnd function based on the system clock. This will provide a much better illusion of randomness. Here's an example:
Randomize x = Rnd() Sgn(numExpr)
Returns 1 if numExpr is greater than 0, -1 if numExpr is less than 0, and 0 if numExpr is equal to 0.
Sin(numExpr)
Returns the sine of numExpr.
Sqr(numExpr)
Returns the square root of numExpr.
Tan(numExpr)
Returns the tangent of numExpr.
VBScript contains a number of built-in functions to work with dates and time.
Date
Returns the current date from the system clock.
DateSerial(year, month, day)
Returns a value of subtype Date to represent the year, month, and day that were passed.
Day(date)
Returns an integer between 1 and 31 to represent the day for the date that was passed.
Hour(time)
Returns an integer between 0 and 23 to represent the hour for the time that was passed.
Minute(time)
Returns an integer between 0 and 59 to represent the minute for the time that was passed.
Month(date)
Returns an integer between 1 and 12 to represent the month for the date that was passed.
Now
Returns the current date and time based on the system clock.
Second(time)
Returns an integer between 0 and 59 to represent the second for the time that was passed.
Time
Returns the current time from the system clock.
TimeSerial(hour,minute,second)
Returns a value of subtype Date to represent the hour, minute, and second that were passed.
Weekday(date [, firstday])
Returns an integer between 1 and 7 that represents the current day of the week for date. By default, Sunday is represented by 1, Monday 2, and so on. If a firstday parameter is passed, another day can be set to be represented by 1. For example, if 2 is passed as the firstday parameter, Monday would be represented by 1.
Year(date)
Returns an integer that represents the year in date; for example, 1996.
VBScript contains many built-in functions to assist in dealing with strings.
In the function declarations below, strExpr can be either a string or an expression that evaluates to a string.
Asc(strExpr)
Returns an integer representing the ANSI code for the first character of strExpr.
Chr(ANSICode)
Returns the character represented by ANSICode.
Hex(number)
Returns a string that represents number in hexadecimal.
InStr([startPos,] string, srchstr [, compType])
Returns the position of the first occurrence of srchstr in string. If startPos is specified, the search begins at that position. The compType parameter can be either 0 or 1. The default value of 0 is case sensitive. A compType of 1 indicates that the search should not be case sensitive.
LCase(strExpr)
Converts strExpr to lowercase and returns it as a string.
Left(strExpr, numChars)
Returns a substring of strExpr that begins at the first position (on the left) and is numChars in length.
Len(strExpr | varName)
If a string expression is passed, Len returns the length of that string. If a variable name is passed, Len returns the number of bytes required to store that variable.
LTrim(strExpr)
Removes all leading spaces from strExpr and returns it as a string.
Mid(strExpr, startPos, numChars)
Returns a substring of strExpr that begins at startPos and is numChars in length.
Oct(number)
Returns a string that represents number in octal.
Right(strExpr, numChars)
Returns a substring of strExpr that begins at the last position (on the right) and is numChars in length.
RTrim(strExpr)
Removes all trailing spaces from strExpr and returns it as a string.
StrComp(strExpr1, strExpr2 [,compType])
Compares strExpr1 and strExpr2. If they are equal, 0 is returned. -1 is returned if strExpr1 is less than strExpr2. 1 is returned if strExpr1 is greater than strExpr2. If either string is null, Null is returned.
The compType parameter can be either 0 or 1. The default value of 0 is case sensitive. A compType of 1 indicates that the search should not be case sensitive.
String(length, character)
Returns a string of repeating character that is length in length.
Trim(strExpr)
Removes all leading and trailing spaces from strExpr and returns it as a string.
UCase(strExpr)
Converts strExpr to uppercase and returns it as a string.
VBScript provides two built-in functions that provide easy access to the user interface: InputBox and MsgBox.
InputBox(prompt [, title][, default][, xPos][, yPos][, helpFile, context])
This function displays a dialog box with a text field. The contents
of the text field are returned. The parameters are defined as
follows:
| Parameter | Description |
| prompt | The prompt that is displayed in the dialog box. |
| title | The text that is displayed on the title bar of the dialog box. |
| default | The default contents of the text field. |
| xPos | The distance (in twips) of the dialog box from the left edge of the screen. |
| YPos | The distance (in twips) of the dialog box from the top of the screen. |
| helpFile | The filename of the help file that should be used for context-sensitive help. |
| context | The context number for the appropriate help topic in helpFile. |
MsgBox(prompt[, buttons][, title][, helpfile, context])
The MsgBox function displays a dialog box with one or
more buttons, as configured by the buttons parameter.
The parameters are defined as follows:
| Parameter | Description | |
| prompt | The prompt that is displayed in the dialog box. | |
| buttons | A number that specifies the number and type of buttons to display in the dialog box. The number is arrived at by adding together four numbers to specify the number and type of buttons, the icon style, the default button, and the modality of the dialog box. The buttons configurations are as follows: | |
| Number/Type Buttons | Effect | |
| 0 | An OK button | |
| 1 | OK and Cancel buttons | |
| 2 | Abort, Retry, and Ignore buttons | |
| 3 | Yes, No, and Cancel buttons | |
| 4 | Yes and No buttons | |
| 5 | Retry and Cancel buttons | |
| Icon | Style | |
| 0 | No icon | |
| 16 | Critical Message icon | |
| 32 | Warning Query icon | |
| 48 | Warning Message icon | |
| 64 | Information Message icon | |
| Default | Button | |
| 0 | First button | |
| 256 | Second button | |
| 512 | Third button | |
| 768 | Fourth button | |
| Modality | Description | |
| 0 | Application Modal | |
| 4096 | System Modal | |
| title | The text that is displayed on the title bar of the dialog box. | |
| helpFile | The filename of the help file that should be used for context-sensitive help. | |
| context | The context number for the appropriate help topic in helpFile. | |
The return value provides the button that was selected:
| Button Selected | |
|
| OK |
|
| Cancel |
|
| Abort |
|
| Retry |
|
| Ignore |
|
| Yes |
|
| No |
VBScript provides several built-in functions for dealing with data types.
CBool(expr)
Returns expr converted to subtype Boolean. If expr is 0, False is returned. True is returned if expr is unequal to 0. A type mismatch runtime error occurs if expr does not represent a numeric value.
CByte(expr)
Returns expr converted to subtype Byte. If expr cannot be converted to subtype Byte, a type mismatch runtime error occurs.
CDate(expr)
Returns expr converted to subtype Date. If expr cannot be converted to subtype Date, a type mismatch runtime error occurs.
CDbl(expr)
Returns expr converted to subtype Double. If expr cannot be converted to subtype Double, a type mismatch or overflow runtime error occurs.
CInt(expr)
Returns expr converted to subtype Integer. If expr cannot be converted to subtype Integer, a type mismatch or overflow runtime error occurs.
CLng(expr)
Returns expr converted to subtype Long. If expr cannot be converted to subtype Long, a type mismatch or overflow runtime error occurs.
CSng(expr)
Returns expr converted to subtype Single. If expr cannot be converted to subtype Single, a type mismatch or overflow runtime error occurs.
CStr(expr)
Returns expr converted to subtype String.
If expr is boolean, either True or False is returned.
If expr is a Date, a string will be returned in the short date format for the particular system.
If expr is of the subtype Error, a string containing the word Error and the error number is returned.
If expr is Null, a runtime error occurs.
DateValue(string)
Returns a Variant of subtype Date to represent the date in string.
IsArray(expr)
Returns a boolean indicating whether expr is an array.
IsDate(expr)
Returns a boolean indicating whether expr can be converted to a Date.
IsEmpty(expr)
Returns a boolean indicating whether expr is empty. The intent of this function is to pass a variable name as expr to determine if it has been initialized.
IsNull(expr)
Returns a boolean indicating whether expr contains Null.
IsNumeric(expr)
Returns a boolean indicating whether expr can be evaluated to a numeric value.
IsObject(expr)
Returns a boolean indicating whether expr references a valid object.
LBound(arrayName[, dimension])
Returns the lower bound of arrayName for the dimension indicated. Because VBScript does not support lower bounds other than zero, this function is not very useful.
TimeValue(string)
Returns a Variant of subtype Date to represent the time in string.
UBound(arrayName[, dimension])
Returns the upper bound of arrayName for the dimension indicated.
Val(strExpr)
Returns the first numeric value found in strExpr. The numeric value must be at the beginning of strExpr. Spaces, tabs, and linefeeds will be removed and periods converted to decimal points. The prefixes &O and &H in strExpr can be used to specify octal or hexadecimal values.
VarType(varName)
Returns a number that indicates the Variant subtype of
varName according to the following:
| Subtype | |
|
| Empty |
|
| Null |
|
| Integer |
|
| Long |
|
| Single |
|
| Double |
|
| Date |
|
| String |
|
| Automation object |
|
| Error |
|
| Boolean |
|
| Variant |
|
| Non-automation object |
|
| Byte |
|
| Array |