Visual Basic Scripting Edition, or VBScript, as it’s commonly called, began its life amid a certain amount of fanfare as a client-side scripting language for web browsers. Its appeal was that it was a subset of Visual Basic for Applications (VBA), the most widely used programming language in the world, and hence promised to make Internet programming easy not only for the huge installed base of VB/VBA programmers, but also for new programmers.
The major source of documentation for VBScript is the Visual Basic Scripting HTML Help file, the official documentation that is included with VBScript itself. While VBScript’s online help is an indispensable resource that most VBScript programmers turn to first, it has a number of limitations:
It offers a rather bare-bones approach to the language. There isn’t a level of detail that allows one to move beyond the basics or to make the documentation useful in troubleshooting and diagnosing sources of error.
The examples rarely, if ever, move beyond the self-evident and obvious.
In a very small number of cases, it incorrectly documents a feature that turns out not to work in VBScript, but that is implemented in VBA. This leads one to suspect that the documentation was originally written for VBA and then was quickly adapted to VBScript.
Since one of the strengths of VBScript is that it allows VBA programmers to leverage their existing skills in learning a new technology, it is peculiar that the documentation totally disregards differences between VBA and VBScript.
In other words, the documentation included with VBScript just doesn’t have the depth of information that you need when you need it. Most of us can get by day-to-day without even opening VBScript Help. But when you need to open the Help file, it’s probably because you’ve either hit an unexpected problem or need to know what the consequences of coding a particular procedure in a particular way will be. However, Help tends only to show you how a function should be included in your code. This is understandable; after all, the help information for any language must be created before that language goes into general use, but it is only general everyday use in real-life situations that highlight how the language can best be used and its problems and pitfalls. Therefore, online help confines itself to the main facts: what the syntax is and, in a general way, how you should implement the particular function or statement.
This book takes up where the Help file leaves off. Contained within these pages are the experiences of professional VB and VBScript developers who have used these languages all day, every day, over many years, to create complex applications. It is these experiences from which you can benefit. Whether you have come to VBScript recently or have been using it since its introduction, there are always new tricks to learn. And it’s always important to find out about the gotchas that’ll getcha!
This book is aimed at experienced VBScript developers or experienced developers coming to VBScript for the first time from another programming or scripting language (including, of course, VB/VBA programmers).
This book is a reference work and not a tutorial—for example, we
won’t explain the concept of a
For...Next loop; as an experienced developer,
you already know this, so you don’t want someone like us insulting your
intelligence. But we will explain in detail how a
For...Next loop works in VBScript, how it
works in practice, what the alternatives to it are, how it can be used
to your best advantage, and what pitfalls it has and how to get around
Although this book is not intended as a tutorial, we have provided in Part I, a concise introduction to the language that focuses not only on the general structure of the VBScript language, but on also its application in the four major environments in which it is used. If you’re learning VBScript as a second language, the introduction combined with the reference is probably all that you’ll need to get started.
VBScript in a Nutshell focuses on the needs of three different audiences: programmers and script developers who are new to VBScript, VB/VBA programmers who are new to VBScript, and VBScript programmers.
This book is based upon the assumption that if you’re new to VBScript, you know one or more other programming languages. The first half of the book leads you through the important areas of VBScript programming, which, while very different from most other languages, are straightforward and easily mastered. We suggest therefore that you read these chapters in order while referring to the Language Reference when necessary.
As an experienced VBScript programmer, you will be able to dip into the book to get the lowdown on the language element that interests you. Appendix A lists all the functions, statements, and object models by category to help you find the relevant section in the Language Reference more easily.
If you know VBA, you know VBScript, since the latter is a subset of the former. On the whole, you’ll find that VBScript is a much “cleaner” language than VBA— many of the archaic elements of VBA (elements that survived as Basic and QBasic evolved into VBA and as statement-based programming evolved into function-based programming and then object-based programming) have been removed from the language. But you’ll also find some incompatibilities, as particular language features that you’re accustomed to in VBA work differently in VBScript. We’ve tried to document those differences in this book.
This book is divided into three parts. The first part of the book, The Basics, is an introduction to the main features and concepts of VBScript programming, as well as an examination of how VBScript is used in its four major scripted environments: Active Server Pages, Windows Script Host, Outlook forms programming, and client-side scripting for Microsoft Internet Explorer (IE).
Even seasoned VB professionals should find items of interest here. If you’re new to VB, this part of the book is essential reading. It is divided into the following chapters:
In this chapter, you’ll find information on the VBScript language and how it fits in to the family of VB products. We’ll also discuss the notion that a scripting language is a kind of “glue” meant to hold together and control various objects. Finally, there’s also a short discussion of the history of VBA.
This chapter details how to create the basic program structures in VBScript; how to implement classes, procedures, functions, and properties and how a program follows proceeds in a VBScript program.
VBScript actually only has a single data type, the variant. This chapter looks at the variant and all its data types and shows how to use them.
On the assumption that we all strive to create robust applications, this chapter covers error handling in your VBScript application and discusses the process of debugging in order to identify and remove program bugs.
This chapter shows how to incorporate VBScript code into an Active Server Page and discusses the IIS object model that you access when creating an ASP application.
Outlook 97 and 98 used VBScript as their only programming language and Outlook forms as their only programmable feature. Outlook 2000 includes two programming languages: VBA for application-level development, and VBScript for forms-based development. In this chapter, we focus on the latter topic by examining the VBScript development environment, discussing how to structure and run Outlook code, and listing some of the basic objects in the Outlook object model.
Programmers, administrators, and power users have long clamored for a “batch language” that would offer the power of the old DOS batch language in a graphical environment. Microsoft’s answer is Windows Script Host (WSH) and a scripting language of your choice. In this chapter, we look at VBScript as the “Windows batch language” by examining program flow and how to launch a WSH script, discussing the WSH object model, and focusing on the XML language elements that you can use to better structure your scripts.
VBScript was first introduced as a scripting language for Internet Explorer, which remains an important, although secondary, area of application for VBScript. In this chapter, we provide a quick overview of how to add script to HTML pages and focus on some of the functionality available through the Internet Explorer object model.
Windows Script Components (WSC) is a technology that allows you to create what appear to be reusable binary COM components with script. Chapter 9 documents WSC and shows how you can use it to create your own binary COM components.
The second part of the book, The Reference, consists of one large chapter. Chapter 10 thoroughly details all the functions, statements, and object models that make up the VBScript language. The emphasis here is on the language elements found in VBScript 5 and 5.5 (which is currently in public beta). See the following section for a detailed explanation of how to use the Language Reference.
The third and final section consists of the following appendixes:
This lists all VBScript functions, statements, and major keywords by category.
This lists the constants built into the VBScript language that are available at all times.
This lists the operators supported by VBScript, along with a slightly more detailed treatment of Boolean and bitwise operators.
This lists the locale IDs by the GetLocale and SetLocale functions.
This documents Script Encoder (a command-line utility that hides source code) and shows how to use it for encoding all VBScript scripts except for those in Outlook forms.
The following template has been used for all functions and statements that appear in Chapter 10:
This section uses standard conventions (detailed in the following section) to give a synopsis of the syntax used for the language item.
Where applicable, this section details whether the item is optional, the data type of the item, and a brief description of the item.
Where applicable, this section provides a very brief description of the value or data type returned by the function or property.
This section provides a short description of what the language element does and when, and why it should be used.
This section describes the main points of how to use the function, presented in the form of a bulleted list to enable you to quickly scan through the list of rules. In the vast majority of cases, this section goes well beyond the basic details found in the VB documentation.
It’s not uncommon for documentation to excel at providing bad examples. How often do we encounter code fragments like the following:
' Illustrate conversion from Integer to Long! Dim iVar1 As Integer Dim lVar2 as Long iVar1 = 3 lVar2 = CLng(iVar1) Response.Write "The value of lVar2 is: " & lVar2
So you won’t find the gratuitous use of examples in this book. We see little point in including a one- or two-line code snippet that basically reiterates the syntax section. Therefore, we’ve tried to include examples only where they enhance the understanding of the use of a language element or demonstrate a poorly documented feature of a language element.
If you’re programming in the Professional or Enterprise Editions of Visual Basic, or in one of the hosted environments (like Microsoft Word or AutoCAD) using Visual Basic for Applications, this section shows you how a particular VBScript language element differs from its VB/VBA counterpart. If no differences are noted, the element functions identically in both environments. This helps you get up to speed with unfamiliar language elements quickly, as well as to get VBA code running under VBScript or VBScript code running under VBA.
This is the most valuable section of the Language Reference, gained from years of experience using the VBA language in many different circumstances. The information included in here will save you countless hours of head-scratching and experimentation. This is the stuff Microsoft doesn’t tell you!
A simple cross-reference list of related or complimentary functions.
Throughout this book, we’ve used the following typographic conventions:
Constant width in body text indicates a language construct
such as a VBA statement (like
Set), an intrinsic or user-defined
constant, a user-defined type, or an expression (like
dElapTime = Timer( )
dStartTime). Code fragments and code
examples appear exclusively in constant-width text. In syntax
statements and prototypes, text in constant width indicates such
language elements as the function’s or procedure’s name, and any
invariable elements required by the syntax.
Constant width italic
Constant width italic in body text indicates parameter and variable names. In syntax statements or prototypes, it indicates replaceable parameters.
Constant width bold
Constant width bold in code listings and examples is used to emphasize particular lines of code.
Italicized words in the text indicate intrinsic or user-defined functions and procedure names. Many system elements like paths and filenames are also italicized, as are new terms where they are defined.
Please address comments and questions concerning this book to the publisher:
|O’Reilly & Associates, Inc.|
|1005 Gravenstein Highway North|
|Sebastopol, CA 95472|
|(800) 998-9938 (in the United States or Canada)|
|(707) 829-0515 (international or local)|
|(707) 829-0104 (fax)|
We have a web page for this book, where we list errata, examples, or any additional information. You can access this page at:
To comment or ask technical questions about this book, send email to:
For more information about our books, conferences, Resource Centers, and the O’Reilly Network, see our web site at:
|http://www.oreilly.com and http://vb.oreilly.com|
We’d like to thank Eric Lippert of Microsoft for his careful and thorough review of the manuscript. Eric went far beyond the call of duty in working to make this a better book.