Differences between VBA and VBScript Code

If you are used to writing VBA (or VB) code, you will find that VBScript (VBS) lacks some of the features you are used to. Table 17.1 (taken from the "Visual Basic for Applications Features not in VBScript" topic in the VBS Help file) lists all the VBA features not supported in VBS code.

TABLE 17.1

Syntax Differences between VBA and VBS Code


Omitted Feature/Keyword

Array Handling

Option Base Declaring arrays with lower bound <> 0


Add, Count, Item, Remove Access to collections using ! character

Conditional Compilation

#Const #If...Then...#Else

Control Flow

DoEvents GoSub...Return, GoTo On Error GoTo On...GoSub, On...GoTo Line numbers, Line labels


CVar, CVDate Str, Val

Data Types

All intrinsic data types except Variant Type...End Type


Date statement, Time statement


LinkExecute, LinkPoke, LinkRequest, LinkSend


Debug.Print End, Stop


Declare (for declaring DLLs) Optional ParamArray Static

Error Handling

Erl Error Resume, Resume Next

File Input/Output

All traditional Basic file I/O


All financial functions

Object Manipulation



Clipboard Collection




Deftype Option Base Option Compare Option Private Module

Select Case

Expressions containing Is keyword or any comparison operators Expressions containing a range of values using the To keyword.


Fixed-length strings LSet, RSet Mid Statement StrConv

Using Objects

Collection access using !

Though this table is useful, it doesn't cover all the differences between VBA and VBS code, nor does it identify the ones that are most likely to cause trouble when converting VBA code to VBS. When working with both Outlook VBS and WSH scripts I have found a number of differences between the two dialects of VB that need to be taken into account when writing VBS code (especially when converting VBA code to VBS). These differences are listed in Table 17.2.

Programming Technique Differences between VBA and VBS Code

VBA Feature

VBS Feature

All named constants can be used as arguments, or for setting values.

Only a very limited number of basic named constants are supported. Use the Object Browser opened from a VBA module window to locate the numeric equivalent of a named constant (in Figure 17.11 you can see that 40 is the numeric equivalent of the olContact named constant).

Variables can (and should) be declared as specific data types, for example Dim appOutlook As Outlook.Application.

Data typing of variables is not supported. Remove all data typing of declarations when converting code from VBA to VBS — for example, instead of Dim appOutlook As Outlook.Application, in VBS the declaration is just Dim appOutlook.

Full error handling is supported, typically using On Error GoTo ErrorHandler with a section specifying the error handling to be done.

Only very limited error handling is supported, using On Error Resume Next. However, you can use various methods of the FileSystemObject to determine whether files exist before performing operations on them, as an alternative to VBA-type error handling for file operations. You can also determine whether or not an object has been set by checking whether it is equal to Nothing (for example, fil Is Nothing).

You can use Debug.Print statements to write data from variables to the Immediate window as the code is executing, for debugging purposes.

Debug.Print is not supported; you must use MsgBox statements instead, which hold up code execution until you click OK (after the script is running correctly, comment out the MsgBox statements).

In a For Each . . . Next statement, use the variable in the Next line (for example, Next fil).

In a For Each . . . Next statement, omit the variable in the Next line (for example, Next).

The Nz function is supported (in Access VBA code, or other VBA code if there is a reference to the Access object library).

The Nz function is not supported, use the IsNull function, or check that a value is equal to "", as substitutes.

To comment out a multi-line code statement using the underscore line continuation character, you only need to put an apostrophe at the beginning of the first line.

To comment out a set of lines using the line continuation character, you must put an apostrophe at the beginning of each line.

0 0

Post a comment