Differences Between VBScript and VBA

VBScript is a subset of the Visual Basic for Applications language. There are several features that VB and VBA programmers have become accustomed to that are not present in VBScript. This does not lessen the usability of VBScript: it only serves to reinforce that VBScript is meant for scripting and not full-blown client/server application development or COM component development. Let’s take a look at a few of the larger differences between VBScript and VBA:

VBScript is an untyped language.

Unlike Visual Basic and Visual Basic for Applications, in which the developer can define the data type of a variable in advance, all variables in VBScript are variants. There are subtypes to handle different types of data, and you can use these as you would the traditional data types in Visual Basic. For more information, see Chapter 3.

VBScript is not compiled.

Although we speak of VBScript code being " compiled” as it is downloaded, VBScript is nevertheless an interpreted language. That means that the code that you write is interpreted into machine language each time you run the script, which imposes a definite performance penalty. Visual Basic programmers who have worked with the language for a long time can remember when Visual Basic was also an interpreted language. Is this a big deal for VBScript? We don’t believe so, since most scripting languages (JavaScript, Perl, Python, etc.) are interpreted rather than compiled. And code portions whose performance are particularly sensitive can be removed from the VBScript code and placed in a compiled COM component that the VBScript code instantiates.

VBScript does not support early binding.

Because variables are untyped and code is not compiled, all external objects instantiated in VBScript code are necessarily late-bound. This has a number of implications. First, late binding typically entails a substantial performance penalty in comparison to early binding. Second, while the properties and methods of early-bound objects can be examined in Visual Basic or hosted VBA environments using the Object Browser, this is not the case with late-bound objects. Finally, the help facilities available for early-bound objects in VB and VBA (like Auto List Members and Auto Quick Info) are not available, making syntax errors more likely and ready access to good documentation all the more necessary.

VBScript does not support named arguments.

Most functions and procedures, VBA supports both positional and named arguments. For example, the VBA MsgBox function can be called using positional arguments as follows:

lResult = MsgBox("Delete this file?", _
          vbYesNo Or vbQuestion Or vbDefaultButton2, _
          "Confirm File Deletion")

A method call using named arguments takes the following form:

lResult = MsgBox(Prompt:="Delete this file?", _
          Title:="Confirm File Deletion", _
          Buttons:=vbYesNo Or vbQuestion Or vbDefaultButton2)

Note that while positional arguments must occur in a predefined sequence, named arguments need not. At least in our experience, more advanced programmers tend to prefer positional syntax, while more novice programmers tend to prefer named arguments.

Given all of this, it is unfortunate that VBScript supports only positional arguments.

VBScript does not have an IDE.

There is no integrated development environment for VBScript that parallels the IDE for Visual Basic and Visual Basic for Applications. Develpment tools are available for all of the environments in which VBScript is used, but all fall short of the power, simplicity, elegance, and ease of use of the VB/VBA IDE.Typically, web developers have had their own environments for writing their code. VBScript for the Web, whether it is client-side or server-side, is embedded inside of a <SCRIPT> tag. This allows web developers to continue to use their tool of choice even when using VBScript. And a wide array of tools for web script development are available. To mention just two from Microsoft, Microsoft Visual Interdev provides a visual interface for web application developers building ASP applications that perform data access. And the Microsoft Script Editor, which is included with Word 2000, provides something resembling (but that falls a little short of ) an IDE for developing HTML pages with client-side and server-side scripts. Scripts for WSH can be created with the use of a simple text editor like Windows Notepad. Outlook comes with its rudimentary IDE (a glorified version of Notepad) for attaching code to Outlook forms.

Get VBScript in a Nutshell now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.