VBScript in a Nutshell

VBScript in a Nutshell

3.0 3
by Paul Lomax, Matt Childs, Ron Petrusha

View All Available Formats & Editions

Lightweight yet powerful, VBScript from Microsoft® is used in four main areas: server-side web applications using Active Server Pages (ASP), client-side web scripts using Internet Explorer, code behind Outlook forms, and automating repetitive tasks using Windows Script Host (WSH). VBScript in a Nutshell, Second Edition delivers current and complete


Lightweight yet powerful, VBScript from Microsoft® is used in four main areas: server-side web applications using Active Server Pages (ASP), client-side web scripts using Internet Explorer, code behind Outlook forms, and automating repetitive tasks using Windows Script Host (WSH). VBScript in a Nutshell, Second Edition delivers current and complete documentation for programmers and system administrators who want to develop effective scripts.Completely updated for VBScript 5.6, WSH 5.6 and ASP 3.0, VBScript In a Nutshell, Second Edition includes updated introductory chapters that will help you keep current with the significant changes since the first edition was published. New chapters introduce the Windows Script Component for creating binary COM components, and the Script Encoder.The main part of the book is a comprehensive reference focusing on VBScript essentials with an alphabetical reference to all statements, keywords and objects, and a section of notes and solutions to real-world gotchas--various undocumented behaviors and aspects of the language--to help you avoid potential problems. Each entry in the reference section details the following:

  • The keyword's syntax, using standard code conventions
  • A list of arguments accepted by the function or procedure, if any exist
  • A discussion of how and where the keyword should be used within the scripting environment
  • A discussion of the differences between the operation of the keyword in Visual Basic or VBA and in VBScript
Regardless of your level of experience programming with VBScript, VBScript in a Nutshell, Second Edition is the book you'll want by your side--the most complete, up-to-date, and easy-to-use language reference available.

Editorial Reviews

This reference book first examines how VBScript is used in active server pages, Windows script host, Outlook forms programming and client-side scripting for Internet Explorer, then documents VBScript statements and functions as well as Scripting Runtime object models. Annotation c. Book News, Inc., Portland, OR (booknews.com)

Product Details

O'Reilly Media, Incorporated
Publication date:
In a Nutshell (O'Reilly)
Sold by:
Barnes & Noble
File size:
4 MB

Read an Excerpt

Microsoft Visual Basic Scripting Edition, commonly known as VBScript, is a relative of the Visual Basic family, which includes the Microsoft Visual Basic Development System (the retail version of Visual Basic in its Enterprise, Professional, and Learning Editions) and Visual Basic for Applications (the language component of Visual Basic, which is included in the individual applications within Microsoft Office and Microsoft Project, as well as in a host of third-party applications).

VBScript is, for the most part, a subset of the Visual Basic for Applications programming language. It was developed so that the millions of Visual Basic developers could leverage their knowledge of VB/VBA Internet scripting. One of the strengths of VBScript is that it uses the same familiar and easy syntax that has made VBA so popular as a programming language, making it very easy to learn for those who have some Visual Basic background. In addition, VBScript is fairly easy to learn for those without any programming experience.

Ironically, VBScript started as a client-side scripting language to create interactive web pages, but in this area it had a major liability: it was and is not supported by Netscape Navigator. Instead, the two major web browsers on the market, Navigator and Microsoft Internet Explorer, both supported a common scripting language, JavaScript, which became the de facto standard for client-side scripting. Despite its failure in this area, however, VBScript rapidly became the major scripting language in three other areas:

  • Active Server Pages (ASP) applications
  • Outlook forms
  • Windows Script Host (WSH) scripts

VBScript's History and Uses

Version 1.0 of VBScript was initially introduced in Microsoft Internet Explorer (MSIE) 3.0, which was released in 1996. Its intended use at that point was to allow web page developers to enhance their pages through client-side scripting. In contrast to plain HTML, which supported the creation of only static web pages, the combination of HTML and client-side script allows the creation of web pages that are both interactive and responsive to the user. For instance, a script could allow the web page to display extended information about hyperlinks as the user's mouse passes over them, or it could be used to validate data entered by the user without submitting it to the server. A script could even be used to generate a web page on the fly, without using any "hard-coded" HTML. The only limitation to VBScript as a language for client-side scripting was that VBScript could be used only inside of Internet Explorer, the only browser to support it, and thus was suitable only for use on corporate intranets that had standardized on MSIE. Using VBScript for clientside scripting on MSIE is discussed in Chapter 8, VBScript witb Internet Explorer.

Version 2.0 of VBScript was introduced in Internet Information Server (IIS) 3.0 in 1997. The most notable additions to the language were "web-friendly" language elements (such as lightweight Format... functions and the Filter, InStrRev, Reverse, and join functions) that in most cases were incorporated into the VBA language only with the release of VBA 6.0. In addition, VBScript 2.0 added support for a number of intrinsic constants to make code more readable and also implemented the Const statement to allow of user-defined constants. Finally, the Create0bject and GetObject functions were added to instantiate external COM objects; these functions, which are inoperative in a client-side scripting environment, are essential for supporting components that are capable of extending a scripted server-side application.

This new version of VBScript was released with IIS to support server-side scripting using ASP. ASP is itself the object model exposed by IIS that allows your script to access information about the client's request and to write to the server's output stream. An ASP application consists of conventional web pages (that is, HTML and possibly client-side script written in any language) along with script that executes on the server. The output of an ASP script most commonly is HTML, which is simply inserted into the output stream returned by the server in response to a client request. This makes ASP important for several reasons. First, it can be used to produce output that is customized for the browser on which it's displayed. Secondly, it provides a very strong web application environment, and particularly one that takes advantage of back-end processing. Along with ASP, Microsoft introduced ActiveX Data Objects (ADO) as its primary data access technology. Developing ASP applications with VBScript is discussed in Chapter 5, VBScript with Active Server Pages.

Although IIS itself is language-independent and supports a number of available scripting languages, it is precisely in this area-in scripting for ASP-that VBScript quickly found its major area of application.

Version 3.0 of VBScript, which was released in 1998, had no new language features. Nevertheless, it was significant for marking the spread of VBScript beyond a scripted web environment. Besides IIS Version 4.0 and MSIE Version 4.0, VBScript was now incorporated into Outlook 98 (an interim release of Outlook that was developed out of sync from the other applications in Microsoft Office), and Windows Script Host 1.0.

Windows Script Host (WSH), which first appeared in the Windows NT 4 Option Pack, exposes some core system resources (like the registry, the network, printers, and the file system) and allows system administrators to write scripts that access or control them using VBScript, Javascript, or any of a number of other scripting languages. The promise here is that administrators will be able to write sophisticated scripts to handle typical administrative tasks. WSH is considerably more powerful than typical Windows Shell scripting, and is also available in Windows 98. Microsoft has built the WSH to help companies address the growing concern of the total cost of administration. In addition, WSH appeals to power users who prefer writing a simple script rather than performing a repetitive task multiple times. Scripting for WSH is discussed in Chapter 7, Windows Script Host.

Microsoft Outlook was originally released in Office 97 as Microsoft's entry into the personal information manager/workgroup messaging market. Outlook featured a number of forms to handle standard MAPI message types (such as messages, contacts, tasks, notes, and appointments) out of the box. However, VBScript made it possible to design new forms and customize their behavior. Although Outlook's latest release, Outlook 2000, includes support for VBA, VBScript remains the programming language for Outlook 2000 forms. Developing Outlook forms with VBScript is covered in Chapter 6, Programming Outlook Forms.

Version 4.0 of VBScript was also released as part of Visual Studio 6.0 in 1998. As in version 3.0, no new language features were present. What was different was the Microsoft Scripting Runtime Library (scrrun.dll ), which now included a File System object model as well as the Dictionary object introduced with VBScript 2.0. The addition of the object model made the library an essential component in any scripted environment.

Version 5.0, shipped with MSIE 5.0 and IIS 5.0 (which shipped with Windows 2000), added a number of new language enhancements, including support for scripted classes using the Class ...End Class construct, support for regular expression searches through the RegExp object, and the ability to dynamically build expressions to be evaluated using the Eval function or executed using the Execute method.

As you can see, even though VBScript's advent as a client-side scripting language was largely unsuccessful, Microsoft remained committed to VBScript as a "lightweight" form of VBA and continued to move the language forward. As a result, it came to be used in a number of environments other than client-side scripts. and in fact has become one of the major scripting languages in use today.

What VBScript Is Used For.' Gluing Together Objects

We've outlined the four major areas in which VBScript is used, but if we were to look at how scripts are written in each of these environments, we'd quickly note far more differences than similarities. (Because of this, we've devoted a separate chapter to each area in which VBScript is commonly used.) Yet this is misleading. If we take a more high-level view, we can see that the role of VBScript and the role of the environment in which its scripts are run are broadly similar, regardless of the environment's unique features.

Typically, scripting languages are described as "glue" languages. That is, they are used to glue things together. That means that the glue itself does relatively little-it simply binds the rest of the script together. The "things" that the scripting language binds together are components or objects-that is, the objects exposed by the environment for which the script is being written (like ASP, MSIE, Outlook, or WSH), as well as objects that are exposed by external applications, environments, or components (such as ActiveX Data Objects, Collaboration Data Objects, Microsoft Word, Microsoft Excel, or custom components created in Visual Basic). A map of a single high-level object (such as the Microsoft Word application, for instance, which is represented by the Application object) along with its child objects is known as an object model.

One type of object model that's particularly suitable for scripting is shown in Figure 1-1. In this particular case, the figure shows the ASP object model. Two features are particularly noteworthy: its flatness and its lack of interdependence. (Contrast it, for example, with the Microsoft Word object model, a portion of which is shown in Figure 1-2.) In particular, a flatter object model and/or one whose objects have a fair degree of independence a number of advantages:

Ease of navigation

Since the object model is flat, you don't have to be concerned with navigating upward and downward through the object hierarchy. This makes coding easier, reduces the time spent debugging, and improves performance.

Ease of instantiating objects

Since objects are independent of one another, you can easily create them or retrieve a reference to them, instead of having to figure out to which portion of the object model you must navigate in order to instantiate that object or which property or method you must call that returns that object...


Individual objects within an object model expose properties, methods, and events. We'll discuss each of these in turn...

Meet the Author

Paul Lomax is the author of Learning VBScript and VB & VBA in a Nutshell (published by O'Reilly & Associates), and technical director of Mentorweb (http://www.mentorweb.net/), a leading web design and hosting company. Over the past two years Paul has created and maintained over 60 commercial web sites for Mentorweb's clients. He is also the driving force behind ShopAssistant, a new NT/ASP based high-end shopping cart/web commerce server (http://www.shopassistant.com/). He has been a programmer for over 12 years and has been a dedicated fan of Visual Basic since version 1. Paul has written systems for financial derivatives forecasting, satellite TV broadcasting, the life insurance industry, and he's written a major materials tracking system for the Oil and Gas industry. He is also responsible for the concept, design, and programming of the successful "Contact" series of national business databases. Paul has also created a web resource dedicated to VBScript at http://www.vbscripts.com/. When not sitting in front of a keyboard, Paul can usually be found behind the wheel of a racing car competing in events around the UK. Paul and his family -- wife Deborah and children Russel and Victoria -- have recently returned to their home in England after several years spent living in the Arabian gulf.

Matt Childs is currently a Vice President with Integrity Solutions Inc., one of Alaska's leading custom software development companies. Matt is responsible for overseeing all in house development, and over the past year has done work with the State of Alaska, and Alaska's two largest telecommunications companies. Matt has worked in the information technology fieldfor the past nine years. He has been a Visual Basic Programmer since Visual Basic 3. Matt spent most of his initial career working for a large transportation company as an Information Technology manager, where he worked on developing custom software solutions and systems integration for the companies largest clients. Matt has industry experience with utilities, express transportation, chemical, petroleum, and retail companies. Matt spends his free time in the winter with his telescope, and during the long Alaska summer days, he enjoys playing softball and spending time with his family. Matt, his wife LeAndra and their daughter Meghan recently returned to Anchorage, Alaska, after spending some time in the southern United States. Matt is also a fiction writer and a freelance journalist.

Ron Petrusha began working with computers in the mid '70s, programming in SPSS (a programmable statistical package) and FORTRAN on the IBM 370 family. Since then, he has been a computer book buyer, editor of a number of books on Windows and UNIX, and a consultant on projects written in dBASE, Clipper, and Visual Basic. Ron also has a background in quantitative labor history, specializing in Russian labor history, and holds degrees from the University of Michigan and Columbia University.

Customer Reviews

Average Review:

Write a Review

and post it to your social network


Most Helpful Customer Reviews

See all customer reviews >

VBScript in a Nutshell 3 out of 5 based on 0 ratings. 3 reviews.
Anonymous More than 1 year ago
Anonymous More than 1 year ago
Anonymous More than 1 year ago