VBScript is a scripting language created by Microsoft. It was originally developed to support client-side Web page development. However, Microsoft has since ported it over to a number of programming environments, including the WSH (Windows Script Host). The WSH is an execution environment designed to support script execution on Windows operating systems. By providing the ability to combine VBScript and the WSH, Microsoft has given power users, programmers, and system and network administrators a scripting language that supports rapid application development using the same friendly and easy-to-learn syntax that made Visual Basic famous.
VBScript is an interpreted scripting language. VBScripts require an execution environment in order to run. Examples of VBScript execution environments include both Internet Explorer and the WSH. Within the context of Web page development, VBScripts are embedded and execute inside HTML (Hypertext Markup Language) pages. When executed by the WSH, VBScripts are stored as plain text files with a .vbs file extension and run from the Windows command prompt or the Windows desktop.
VBScript is an excellent language for improving client-side Web page development and for developing small scripts that automate tasks on Windows operating systems. It is also a good choice for quickly developing small applications and utilities or for prototyping new applications.
VBScripts are limited by the constraints imposed by their execution environment. This means that while a VBScript embedded within an HTML page can validate form contents and control browser activity, it cannot access local disk drives or other resources on the computer on which it executes. On the other hand, while VBScripts that are executed by the WSH can access local drivers and printers, they cannot work with browser-based resources.
Examples of tasks that VBScripts embedded inside Web pages can perform include the following:
VBScripts designed to execute with the WSH have an entirely different purpose. Examples of tasks that VBScripts run by the WSH can perform include the following:
As far as programming languages go, VBScript is still relatively new. Microsoft first introduced it in 1996 as a client-side Web page development scripting language for Internet Explorer 3.0. However, VBScript's arrival was preceded by another client-side scripting language, known at the time as LiveScript and later renamed JavaScript. JavaScript's head start, combined with concern over the proprietary nature of VBScript, led to a slow start for Microsoft's new scripting language. In addition, Netscape never added support for VBScript to its browser, making JavaScript the only universally supported client-side scripting language. As a result, while JavaScript was quickly accepted by the Internet community, VBScript's success was slow in coming.
VBScript's popularity began to increase when Microsoft released VBScript version 2.0 and enabled it to provide IIS 3.0 (Internet Information Server 3.0) with a server-side Web development scripting language. By embedding VBScripts inside ASPs (Active Server Pages), Web developers were able to use VBScript as a means of accessing data stored on server-side databases and to more easily provide dynamic Web content.
VBScript version 3.0 was released as a component supplied with numerous Microsoft products. This list of products included:
Of all these environments, it was the WSH where VBScript had the greatest impact. Individuals with a Visual Basic background quickly found that they now had a powerful scripting tool that supported rapid application development and task automation.
VBScript version 4.0 was introduced as part of the Microsoft Visual Studio application development suite and given the ability to access the Windows file system. VBScript version 5.0 was released along with Windows 2000. In addition, Microsoft distributed it as a part of WSH 2.0 and Internet Explorer 5.0. When Microsoft released Windows XP and Internet Explorer 6.0 in 2001, it included VBScript 5.6 and WSH 5.6, both of which represent the current releases of these products.
VBScript is one of three languages that make up the Visual Basic family of programming languages. These three languages include:
While all three of these languages are closely related and share many of the same features, each has been designed to suit a different developmental need. Visual Basic is designed to support the development of new applications, whereas VBA is designed to provide an automation facility for specific applications. VBScript, on the other hand, is designed as a general purpose scripting language for deployment in a number of different environments.
Visual Basic
Visual Basic was introduced in 1991 and was an instant hit. New programmers found it intuitive and easy to learn, whereas experienced programmers found it reliable and powerful. The current version of Visual Basic is called Visual Basic .NET. This name reflects the language's support for Microsoft's .NET framework.
Note |
.NET is a Microsoft framework that supports the exchange of data over a number of different mediums, including local area networks and the Internet. Visit http://www.microsoft.com/net to learn more about Microsoft's .NET framework. |
Visual Basic applications are compiled programs and can execute independently on any Windows operating system. As a compiled program, all the statements that make up a Visual Basic program are converted into and stored as binary code, allowing them to run quickly. In order to create Visual Basic applications, programmers must first learn how to work with Visual Basic's IDE (Integrated Development Environment). An IDE is a development tool that assists programmers in creating new programs by supplying a compiler, a debugger, a help system, and project management tools.
Because of its IDE, Visual Basic is not well suited to the development of small automation scripts. Instead, Visual Basic is best suited to developing applications that require stand-alone execution and that justify the time and effort required to create them.
Tip |
To find out more about Visual Basic .NET, refer to Microsoft Visual Basic .NET Professional Projects, by Pooja Bembey and Kuljit Kaur with NIIT (Premier Press, 2002). |
Note |
You can learn more about Visual Basic at the Microsoft Visual Basic Web site at http://msdn.microsoft.com/vbasic. |
Visual Basic for Applications
The second member of the Visual Basic family of applications is VBA, which Microsoft introduced in 1993. VBA provides a subset of Visual Basic's functionality for a particular application. VBA is designed to provide the ability to customize a host application such as Microsoft Excel or Access. For example, using VBA for Microsoft Access, a programmer can develop an entire application that uses the Microsoft Access database as its repository. VBA is designed to provide programmers with a foundation from which to begin their application development, rather than requiring them to start from scratch.
The current version of VBA is version 6.3. It was released in 2001 and supports the following list of applications:
Tip |
To find out more about VBA for Microsoft Excel, refer to Microsoft Excel VBA Professional Projects, by Duane Birnbaum (Premier Press, 2003). |
Tip |
To find out more about VBA for Microsoft Access, refer to Microsoft Access VBA Programming for the Absolute Beginner, by Michael Vine (Premier Press, 2002). |
Note |
You can learn more about VBA at the Microsoft Visual Basic Web site at http://msdn.microsoft.com/vba. |
Despite VBScript's initial lackluster start, Microsoft maintained a strong commitment to VBScript, continually updating and improving it. Since its inception, Microsoft has ported VBScript over to a number of different environments, demonstrating VBScript's flexibility and adaptability. As a result, VBScript has grown increasingly popular over the years, providing programmers with multiple avenues for exploiting their VBScript skills.
The following list outlines the major execution environments that currently support VBScript:
VBScript was originally developed as a client-side Web scripting language, and over the years, Microsoft has continued its commitment to this technology. When used for client-side Web page development, VBScripts are generally embedded inside HTML pages and downloaded into client browsers as part of a Web page.
Like the HTML pages that contain them, VBScripts can be created using any editor that can saves files in plain text. For example, the Microsoft Notepad application makes for an acceptable editor. However, there are plenty of good HTML editors available today that also include support for VBScript development.
Tip |
One good example of an HTML and script editor is HomeSite. HomeSite provides numerous features, including:
You can learn more about HomeSite by visiting the Macromedia Web site at http://www.macromedia.com/software/homesite. |
VBScript is supported by the Microsoft Internet Explorer browser but not by Netscape Communicator. VBScript is also supported by a larger number of third-party browsers, which are based at least in part on Internet Explorer. This provides VBScript with a significantly broader base of support on the Internet than is generally recognized.
For example, it is not surprising that the MSN Explorer browser supplied by Microsoft to its MSN Internet customers supports VBScript. Other examples include both CompuServe and AOL who supply their customers with custom browsers based on Internet Explorer. The following list provides a glimpse at the number of Internet Explorer compatible browsers currently available.
Note |
You can learn more about Internet Explorer at the Microsoft Internet Explorer Web site, http://www.microsoft.com/windows/ie. |
VBScript integration with Web content is usually achieved by embedding VBScripts directly inside HTML pages by placing VBScript statements inside the HTML tags. You can use these tags to embed VBScripts into both the HEAD and BODY sections of any HTML page.
Using the tags, you can add VBScripts to HTML pages in three different ways, as outlined below.
Loading VBScripts
The syntax required to embed a VBScript into an HTML page so that it automatically runs when the page is loaded is outlined below.
The tag marks the end of the VBScript. Between the opening tags you may include as many VBScript statements as required. For example, the following HTML page contains two embedded VBScripts, one in the BODY section and another in the HEAD section.
Script 1.1.html - A HTML page with two embedded VBScripts
The VBScript in the BODY section is automatically executed when the HTML page is loaded by the browser. It displays a message in a pop-up dialog box, as demonstrated in Figure 1.1. The second VBScript is defined as a subroutine in the HEAD section and is executed when called by the VBScript located in the BODY section, displaying the pop-up dialog box shown in Figure 1.2.
Figure 1.1: A demonstration of a pop-up dialog box displayed when the HTML page that defines it is loaded by Internet Explorer
Figure 1.2: Pop-up dialog box generated by a VBScript subroutine called by another VBScript
Note |
The previous example, along with all of the example scripts that you will see in this book, can be downloaded from the book's companion Web site, http://www.premierpressbooks.com/downloads. The name of this example is displayed inside the <TITLE></TITLE> tags and is Script 1.1.html. To test the above example, download it and load it in an Internet Explorer compatible Web browser. |
Responding to Events
VBScripts embedded inside HTML pages can also be set to execute based on events triggered by visitors. Examples of browser events include visitors clicking on a form button, a link, or a graphic image. By creating VBScripts that react to browser events, you can add substantial interactivity to Web pages. For example, you can ask the visitor for confirmation to continue after clicking on a link where secure information may be stored. You could also trigger a VBScript that performs form validation after the visitor clicks on the Submit button and to provide instructions in the event that the form is not properly filled out.
In order to set up this kind of reference, you must use the following syntax when defining the tags.
The FOR parameter identifies the HTML object for which the VBScript is to be associated. The EVENT parameter identifies a event handler that specifies the type of event that will trigger the VBScript's execution. For example, the onDblClick event handler can be used to specify that a double mouse click is required to trigger the script's execution. Table 1.1 provides a list of different browser events and their associated event handlers.
Property |
Event |
Description |
---|---|---|
Abort |
onAbort |
Executes when the visitor aborts an image while it is loading |
Blur |
onBlur |
Executes when the currently selected object loses focus |
Change |
onChange |
Executes when the visitor changes an object |
Click |
onClick |
Executes when the visitor clicks an object |
DblClick |
onDblClick |
Executes when the visitor double-clicks an object |
DragDrop |
onDragDrop |
Executes when the visitor drags and drops an object onto a frame or window |
Error |
onError |
Executes when an error occurs on the HTML page |
Focus |
onFocus |
Executes when a visitor selects an object |
KeyDown |
onKeyDown |
Executes when a visitor presses down on a key |
KeyPress |
onKeyPress |
Executes when a visitor presses and releases a key |
KeyUp |
onKeyUp |
Executes when a visitor releases a key |
Load |
onLoad |
Executes when an HTML page or image finishes loading |
MouseDown |
onMouseDown |
Executes when a visitor presses a mouse button |
MouseMove |
onMouseMove |
Executes when a visitor moves the pointer |
MouseOut |
onMouseOut |
Executes when a visitor moves the pointer off of an object |
MouseOver |
onMouseOver |
Executes when a visitor moves the pointer over an object |
MouseUp |
onMouseUp |
Executes when a visitor releases a mouse button |
MouseWheel |
onMouseWheel |
Executes when a mouse wheel is rotated |
Move |
onMove |
Executes when the visitor moves a frame or window |
Reset |
onReset |
Executes when a visitor clicks on a reset button |
Resize |
onResize |
Executes when the visitor resizes a frame or window |
Select |
onSelect |
Executes when a visitor selects the contents of a form text field |
Submit |
onSubmit |
Executes when a visitor clicks on a submit button |
Unload |
onUnload |
Executes when a visitor closes the browser window or frame or loads a different URL |
The following example demonstrates how to set up a VBScript inside an HTML page to react when the visitor clicks on a form button.
Script 1.2 - Example of a VBScript triggered by a browser event
In this example, a form called TestForm is defined that contains a single form element, a button called TestButton that displays the message Click on Me. A VBScript has been added to the HTML page. It has been set up to execute only when the click event occurs for TestButton, as highlighted in the example. When this occurs, the pop-up dialog box shown in Figure 1.3 is displayed.
Figure 1.3: A demonstration of a pop-up dialog box triggered by the VBScript onClick event handler
Referencing External Scripts
VBScript also provides the ability to separate your VBScripts from your HTML by storing VBScripts as external files with a .vbs file extension and then providing a reference to them. This option keeps your HTML and VBScript code separate, potentially making both more readable and manageable. This option also allows you to create VBScript that can be called upon and shared by any number of HTML pages.
The following example demonstrates how to reference an external script from within an HTML page.
Script 1.3 - An example of how to execute an external VBScript
As you can see, the src="books/2/441/1/html/2/Script 1.4.vbs" parameter identifies the name of the external VBScript file. The external VBScript file can contain any number of VBScript statements. It must be saved as a plain text file and cannot contain any HTML whatsoever. For example, if the following VBScript statement were saved as Script 1.4.vbs, it would display the pop-up dialog box shown in Figure 1.4 when executed by the HTML example.
Figure 1.4: A pop-up dialog box displayed by an externally referenced VBScript
MsgBox "This message is being displayed by an external VBScript."
The WSH is a programming environment that supports the execution of scripts directly from the Windows desktop or command prompt. By default, the WSH supports both the VBScript and JScript scripting languages, although third-party support for other scripting languages is available. The WSH provides scripts with an environment in which they can execute. In addition, the WSH provides scripts with direct access to a number of Windows resources, including:
The current version of the WSH is version 5.6. This version of the WSH was supplied with both versions of the Windows XP operating system. Different versions of the WSH shipped with different versions of Windows, as shown in Table 1.2.
Operating System |
WSH Supported |
Version |
---|---|---|
Windows 95 |
Yes |
N/A |
Windows 98 |
Yes |
1.0 |
Windows Me |
Yes |
2.0 |
Windows NT |
Yes |
N/A |
Windows 2000 |
Yes |
2.0 |
Windows XP |
Yes |
5.6 |
While Windows 95 and NT 4.0 were never shipped with the WSH, these two operating systems do support it. In addition, you can upgrade the WSH to version 5.6 on any Windows operating system starting with Windows 95.
Note |
To download the most current release of the WSH, visit http://msdn.microsoft.com/scripting. |
The WSH is a 32-bit application. It is composed of three major components. These components include:
Scripting engines provide WSH scripts with an interpreter that translates script statements into executable code. The WSH provides two different execution hosts. These hosts provide the ability to run scripts from either the Windows desktop or the Windows command prompt. Finally, the core object model provides scripts with access to Windows resources. Figure 1.5 depicts the relationship of each of these three components to one another.
Figure 1.5: The components that comprise the WSH
Scripting Engines
Scripting engines provide the WSH with the ability to interpret statements written in a particular scripting language to a format that can be executed by an execution host. By default, Microsoft supplies the following scripting engines with the WSH:
Microsoft designed the WSH in a modular fashion. This allows Microsoft to update the WSH on a component-by-component basis without having to rework the entire application in order to add new features and capabilities. This same architecture allows third-party software developers to add support for additional scripting engines. This has already been done for a number of different scripting languages, as shown in Table 1.3.
Language |
Name |
Web Site |
---|---|---|
Perl |
ActivePerl |
http://www.activestate.com |
Python |
ActivePython |
http://www.activestate.com |
REXX |
Object REXX |
http://www-3.ibm.com/software/ad/obj-rexx |
The choice of which scripting language to use when automating a Windows task is entirely up to the programmer. However, VBScript has emerged as the most popular scripting language currently supported by the WSH.
Execution Hosts
Once a scripting engine interprets a script, the script is ready to be executed. This is the job of the script execution host. The WSH supplies two different script execution hosts, as outlined below.
The only difference between the two script execution hosts is the ability of the WScript.exe execution host to display output graphically. Otherwise, both script execution hosts provide equivalent functionality. To run a script from the Windows desktop using the WScript.exe execution host, simply locate it and double-click on its icon. Like the Cscript.exe execution host, the WScript.exe execution host can be used to run scripts from the Windows command prompt. You can run a script from the Windows command prompt using the WScript.exe execution host as demonstrated below.
WScript scriptname
Likewise, you can run a script form the Windows command prompt using the CScript.exe execution host, as demonstrated below.
CScript scriptname
If you have any arguments that you need to pass to the script, type them separated by spaces after the script's name, as demonstrated below.
CScript scriptname arg1 arg2 arg3
As a general rule, it is typical to use the CScript.exe execution host when a script will be run from the Windows command prompt or when it will be run by the Windows scheduling service and no interaction is required with the user. When user interaction is required, it's generally better to do so using pop-up dialog boxes rather than the Windows command prompt, making the WScript.exe execution host the better choice.
The Core Object Model
The WSH's core object model provides scripts with the ability to programmatically interact with and manipulate Windows resources, which the object model exposes or represents as objects. The WSH core object model is implemented as an ActiveX control called WSH.OCX. Examples of the objects exposed by the WSH object model include drives, printers, files, and shortcuts. Every object exposed by the WSH object model has a collection of properties and methods. VBScripts can use these properties and methods to interact with and control these resources.
Properties are object-specific attributes that describe or modify a particular feature or component of the object. For example, a desktop shortcut has a file name and a file extension. The values of properties can be viewed and changed, thus having a direct impact on the shortcut. A method is a built-in WSH function. Methods can be used to take action on an object. For example, methods exist that allow scripts to create and delete shortcuts.
Using the methods and properties belonging to objects exposed by the WSH core object model, a VBScript can be developed that can automate tasks on any computer running Windows 95 or later where the WSH has been installed.
VBScripts executed by the WSH are saved as plain text files with a .vbs file extension. Unlike Visual Basic or VBA, VBScript does not provide a built-in IDE. You can use any editor to create VBScript files so long as the editor can save the files in plain text. For example, the Notepad application supplied with all versions of Windows suits this purpose.
Note |
There are a number of third-party VBScript editors that you might want to investigate that will make VBScript development easier when working with the WSH. These editors provide features such as line numbers, color statement coding, and the ability to test scripts without exiting the editor. One such example is VbsEdit, which you will find at http://www.adersoft.com. |
For example, the following two VBScript statements represent a small VBScript.
UserName = InputBox("What is your name?") MsgText = MsgBox("Greetings " & UserName)
The first statement collects the user's name, and the second statement then uses the user's name to display a custom greeting. Once saved and executed, the VBScript will interact with the user as demonstrated in Figures 1.6 and 1.7.
Figure 1.6: Using the WSH to run a VBScript that collects a user's name
Figure 1.7: Using the user's name to display a custom welcome message
A good understanding of how to work with the Windows command prompt is an important prerequisite for any VBScript programmer. The Windows command prompt is a text-based interface between you and the operating system. It can be used as an effective alternative to working with the Windows GUI (graphical user interface).
To access the Windows command prompt on a computer running Windows XP, click on Start, All Programs, Accessories and then Command Prompt. This starts a Windows console, as shown in Figure 1.8.
Figure 1.8: To work with the Windows command prompt, you must first open a Windows console
Tip |
To quickly access the Windows command prompt on any Windows computer, click on Start and then Run. This displays the Run dialog box. Next type CMD and then click on OK. |
By default, the Windows command prompt appears as a drive letter followed by a colon, a backslash, and the greater than (>) character. Just to the right of the command prompt will be a blinking cursor. The blinking cursor indicates that the command prompt is ready to accept a new command. You may now type any Windows command and press the Enter key as demonstrated below.
C:>ver Microsoft Windows XP [Version 5.1.2600] C:>
As you can see, the VER command was entered. This command displays information about the version of Windows being run. The output produced by the command was displayed in the Windows console, and then the command prompt was displayed again, along with the blinking cursor indicating that it was ready to accept a new command.
Note |
For a detailed listing of Windows commands, refer to Appendix A, "Windows Command Reference." |
The following VBScript statement represents a one-line VBScript file.
WScript.Echo "This message is being displayed by a test VBScript"
When executed from the Windows command prompt using the CScript.exe execution host, this VBScript displays the output shown in Figure 1.9.
Figure 1.9: Examining the results displayed by a VBScript run using the CScript.exe execution host
When done working with the Windows command prompt, you can close the Windows console like any other Windows application. This is done by clicking on the Close icon in the upper right-hand corner of the dialog box or by right-clicking on the command prompt icon in the upper left-hand corner and selecting Close. You may also close the Windows console by typing the EXIT command at the Windows command prompt and pressing the Enter key.
In this chapter, you learned about the origins of VBScript. This included an overview of the Visual Basic family of programming languages as well as an explanation of the various environments to which Microsoft has ported VBScript. In addition, you learned the basic steps involved in integrating VBScripts into HTML pages. You also learned about the basic architecture behind the WSH. Finally, you learned how to create and execute scripts using both the WSH and Internet Explorer.
Part I - Introducing Microsoft VBScriptBasics
Part II - Professional Project 1 Desktop Administration Using VBScript and the WSH
Part III - Professional Project 2 Analyzing Application Logs
Part IV - Professional Project 3 Creating a Centralized Report Management Station
Part V - Professional Project 4 Reporting Application Summary Data via the Web
Part VI - Introducing Microsoft VBScriptBasics