This appendix contains a summary or 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 (except in the array syntax section). 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 |
VBScript operators can be characterized as follows:
variable = value | Assignment of value to variable |
= | Equal |
<> | Not equal |
> | Greater than |
>= | Greater than or equal to |
< | Less than |
<= | Less than or equal to |
Is | Equal (objects) |
var1 + var2 | Addition |
var1 - var2 | Subtraction |
var1 * var2 | Multiplication |
var1 / var2 | Division |
var1 & var2 | Modulus |
var1 \ var2 | Integer Division |
- | Negation |
var1^exp | Exponentiation |
string1 & string2 | String Concatenation |
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 |
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 supports the following conditional structures:
If condition Then ' statements for true condition [Else] ' statements for false condition End If 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
VBScript supports the following loop structures:
For var = init To final [Step step] ' statements to execute while looping Next While condition ' statements to execute while looping Wend Do While condition ' statements to execute while looping [Exit Do] ' statements to execute while looping Loop Do Until condition ' statements to execute while looping [Exit Do] ' statements to execute while looping Loop Do ' statements to execute while looping Loop [Until|While]
VBScript supports two types of procedures, Sub and Function:
Sub SubName([param1][,param2] [,paramN]) ' sub procedure statements End Sub Function fnName([param1][,param2] [,paramN]) ' function statements; fnName = expr End Function
VBScript contains a number of built-in functions. The VBScript functions can be characterized as follows:
VBScript provides a wide range of mathematical functions for use in your scripts. In the following function declarations, numExpr can be either a number or an expression that evaluates to a number. The return values of the trigonometric
functions are in radians.
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 enumExpr where e is Euler's constant.
Fix(numExpr)
Returns the integer portion of numExpr. If the number is negative, the next greater integer will be returned.
Int(numExpr)
Returns the integer portion of numExpr. If the number is negative, the next lower integer will be returned.
Log(numExpr)
Returns the natural log of numExpr.
Rnd([numExpr])
Returns a pseudo-random number. The number is not really random since the same seed will always produce the same result. If numExpr is used, the result will be as follows:
If numExpr = 0, then Rnd returns the last random number generated.
If numExpr > 0, then Rnd returns the next random number in the sequence.
If numExpr < 0, then Rnd returns a random number based on the seed, numExpr. The same seed will always return the same number.
The Randomize statement will generate a seed for the Rnd function based on the system clock. This will provide for a much better illusion of randomness, as shown in the following 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.
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, then another day can be set to be represented by 1.
For example, if 2 is passed as the firstday parameter, then Monday would be represented by 1.
Year(date)
Returns an integer that represents the year in date, such as 1996.
VBScript has a wide array of built-in functions to assist you 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, then the search will begin 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 be case insensitive.
LCase(strExpr)
Converts strExpr to lower case 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, varName, is passed, then 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 a zero is returned. A -1 is returned if strExpr1 is less than strExpr2. A 1 is returned if strExpr1 is greater than strExpr2. If either string is Null, then
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 be case insensitive.
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 upper case and returns it as a string.
InputBox(prompt [, title][, default][, xPos][, yPos][, helpFile, context])
This function will display a dialog box with a text field. The contents of the text field will be returned. The parameters are defined as follows:
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 will display a dialog box with one or more buttons, as configured by the buttons parameter. The parameters are defined as follows:
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 4 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 | |
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:
Return Value | Button Selected |
1 | OK |
2 | Cancel |
3 | Abort |
4 | Retry |
5 | Ignore |
6 | Yes |
7 | No |
VBScript provides many functions to help in dealing with datatypes. The functions below that are pre-fixed with C are used to convert a value to a Variant of a specific subtype. The functions pre-fixed with Is can be used to determine if an expression
can be converted to a specific subtype.
CBool(expr)
Returns expr converted to subtype Boolean. If expr is 0, then False will be returned. True will be returned if expr is unequal to 0. A type mismatch runtime error will occur if expr does not represent a numeric value.
CByte(expr)
Returns expr converted to subtype Byte. If expr can not be converted to subtype Byte a type mismatch runtime error will occur.
CDate(expr)
Returns expr converted to subtype Date. If expr can not be converted to subtype Date a type mismatch runtime error will occur.
CDbl(expr)
Returns expr converted to subtype Double. If expr can not be converted to subtype Double a type mismatch or overflow runtime error will occur.
CInt(expr)
Returns expr converted to subtype Integer. If expr can not be converted to subtype Integer a type mismatch or overflow runtime error will occur.
CLng(expr)
Returns expr converted to subtype Long. If expr can not be converted to subtype Long a type mismatch or overflow runtime error will occur.
CSng(expr)
Returns expr converted to subtype Single. If expr can not be converted to subtype Single a type mismatch or overflow runtime error will occur.
CStr(expr)
Returns expr converted to subtype String.
If expr is Boolean, then either True or False is returned.
If expr is a Date, then a string will be returned in the short date format for the particular system.
If expr is subtype Error, then a string containing the word Error and the error number will be 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 or not expr is an Array.
IsDate(expr)
Returns a Boolean indicating whether or not expr can be converted to a Date.
IsEmpty(expr)
Returns a Boolean indicating whether or not 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 or not expr contains Null.
IsNumeric(expr)
Returns a Boolean indicating whether or not expr can be evaluated to a numeric value.
IsObject(expr)
Returns a Boolean indicating whether or not expr references a valid object.
LBound(arrayName[, dimension])
Returns the lower bound of arrayName for the dimension indicated. Since 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 table:
Returned Value | Subtype |
0 | Empty |
1 | Null |
2 | Integer |
3 | Long |
4 | Single |
5 | Double |
7 | Date |
8 | String |
9 | Automation object |
10 | Error |
11 | Boolean |
12 | Variant |
13 | Non-automation object |
17 | Byte |
8192 | Array |