TABLE 17.2
Programming Technique Differences between VBA and VBS Code
VBA Feature VBS Feature
All named constants can be used Only a very limited number of basic named constants are
as arguments, or for setting values. 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
olContactnamed constant).
Variables can (and should) be Data typing of variables is not supported. Remove all data typing of
declared as specific data types, declarations when converting code from VBA to VBS — for example,
for example Dim appOutlook As instead of Dim appOutlook As Outlook.Application,in
Outlook.Application. VBS the declaration is just Dim appOutlook.
Full error handling is supported, Only very limited error handling is supported, using On Error
typically using On Error GoTo Resume Next. However, you can use various methods of the
ErrorHandlerwith a section FileSystemObject to determine whether files exist before performing
specifying the error handling to operations on them, as an alternative to VBA-type error handling for
be done. 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 Debug.Printis not supported; you must use MsgBoxstatements
statements to write data from instead, which hold up code execution until you click OK (after the
variables to the Immediate window script is running correctly, comment out the MsgBoxstatements).
as the code is executing, for
debugging purposes.
In a For Each... Nextstatement, In a For Each... Nextstatement, omit the variable in the
use the variable in the Nextline Nextline (for example, Next).
(for example, Next fil).
The Nzfunction is supported (in The Nzfunction is not supported, use the IsNullfunction,
Access VBA code, or other VBA code or check that a value is equal to “”, as substitutes.
if there is a reference to the Access
object library).
To comment out a multi-line code To comment out a set of lines using the line continuation character,
statement using the underscore line you must put an apostrophe at the beginning of each line.
continuation character, you only
need to put an apostrophe at the
beginning of the first line.
Creating Standalone Scripts with Windows Script Host 17