VBScript uses a small subset of the features you'll find in the VBA type library. The reason why VBScript is a scaled-down version of Visual Basic is that VBScript's code is interpreted on the fly; there are also security reasons. This section briefly discusses each of these issues.
Prior to the introduction of VBScript, JavaScript was established as the industry standard for Web-page scripting, but JavaScript's similarity to C++ made it difficult for most programmers to use. The main purpose for Microsoft's developing VBScript was to provide a Web scripting language compatible with Microsoft's industry standard for Windows development, Visual Basic. However, this new language was going to be embedded into Web pages, so it needed to be scaled-down enough so that it could be interpreted as quickly as JavaScript. To accomplish this, rarely used features of the Visual Basic language needed to be removed.
Since the introduction of VB 1.0, many features have been added to Visual Basic to simplify common programming tasks, but many of the tasks accomplished by these new features could have been accomplished in VB 1.0 with a few extra lines of code. Therefore, the goal of VBScript was to give the VB programmer a small set of fundamental features that could be used together to accomplish more complex tasks. This means that programming in VBScript requires a little more effort and ingenuity than required by VB, but the end result can be just as powerful.
Another reason why VBScript was scaled down from its VB/VBA siblings was for security concerns. If Microsoft let VBScript programmers call the Windows API or perform file I/O, then a hacker easily could write a virus in VBScript to crash every system that executed his or her VBScript Web page. In order to reduce the chances of someone's writing malicious scripts, all the features of VB that potentially could be abused were removed.
Perhaps the biggest difference between VBScript and other versions of VB is that VBScript supports only the Variantdata type. This means that a statement like "Dim x As Integer" would trigger an error in VBScript, because the Integer data type is not supported. This change was made because it simplifies the code for the interpreter, thus making execution much faster. However, this change really isn't a limitation for VB programmers, because the Variantdata type is flexible enough to support all the data types used by VB.
Another major omission from VBScript is the many predefined constants found in the VBA type library. For example, the Greetings routine shown in demonstrates how the code looks in VB or VBA; shows how the Greetings routine would look in VBScript.
Listing 25.2 - GREETINGS.VBS - VB Code Works in VBScript, but not Without Minor Modifications
Sub Greetings(YourName) Dim Reply Reply = MsgBox("Hello " & YourName & _ ", are you ready to compute?", 32 + 4) If Reply = 6 Then MsgBox "Well then, let's get busy!", 64 Else MsgBox "I'm sorry to hear that, but you must.", _ 64 End If End Sub
Notice how in Listing 25.2 all the constants now appear as their numeric values. Also notice that we no longer use specific data types. Instead, we just declare a variable and use it. While this may be a little different from what you are used to, you can see that your knowledge of VB goes a long way in creating great VBScript code.
The omission of predefined constants in VBScript is something you can actually get around, in order to make porting your code as easy as possible. Although VBScript does not support defining constants, you can simply define variables that use the same name and leave your original VB code virtually unchanged. The code in demonstrates this technique.
Listing 25.3 - GREETINGS2.VBS - Defining Variables for Missing Constants Makes Porting Easier
Sub Greetings(YourName) Dim Reply Dim vbQuestion, vbYesNo, vbYes, vbInformation vbQuestion = 32 vbYesNo = 4 vbYes = 6 vbInformation = 64 Reply = MsgBox("Hello " & YourName & _ ", are you ready to compute?", vbQuestion + vbYesNo) If Reply = vbYes Then MsgBox "Well then, let's get busy!", vbInformation Else MsgBox "I'm sorry to hear that, but you must.", _ vbInformation End If End Sub
By declaring the constants in your VB programs, porting becomes even easier. The only changes made in from the original code in were the "constants" list and removing any references to data types (such as strings and integers).
Table 25.2 shows a complete listing of all VBA language elements missing from VBScript. This information was taken directly from the VBScript documentation available at http://www.microsoft.com/vbscript/us/vbslang/vsgrpnonfeatures.htm .
Table 25.2 - VBA Features Not in VBScript
Category | Omitted Feature/Keyword |
---|---|
Array Handling | Arrayfunction Option Base Private, Public Declaring arrays with lower-bound <> 0 |
Collection | Add, Count, Item, Remove Access to collections using !character (for example, MyCollection!Foo) |
Conditional Compilation | #Const #If...Then...#Else |
Constants/Literals | Const All intrinsic constants Type-declaration characters (for example, 256&) |
Control Flow | DoEvents For Each...Next GoSub...Return, GoTo On Error GoTo On...GoSub, On...GoTo line numbers, line labels With...End With |
Conversion | CCur, CVar, CVDate Format Str, Val |
Data Types | All intrinsic data types except Variant Type...End Type |
Date/Time | Datestatement, Timestatement Timer |
DDE | LinkExecute, LinkPoke, LinkRequest, LinkSend |
Debugging | Debug.Print End, Stop |
Declaration | Declare(for declaring DLLs) Property Get, Property Let, Property Set Public, Private, Static ParamArray, Optional New |
Error | Handling Erl Error On Error...Resume Resume, Resume Next |
File Input/Output | All |
Financial | All financial functions |
Object Manipulation | CreateObject GetObject TypeOf |
Objects | Clipboard Collection |
Operators | Like |
Options | Deftype Option Base Option Compare Option Private Module |
Strings | Fixed-length strings LSet, RSet Midstatement StrConv |
Using Objects | TypeName Collection access using ! |
At first, this long list of omissions may be intimidating, but don't give up. The most important features of VB are included in VBScript, so you still can write powerful scripts. The only difference is that now you'll have to be more creative, which requires you to think in a new way. You may find that some of the tricks you invent to overcome these limitations are so good that you want to port them back to your VB applications. VBScript is a new challenge, but it can be a lot of fun.