VBScript Part 1 - Introduction
VBScript is a scripting language. A VBScript can be executed in various ways
- Using Windows Script Host (wscript.exe and cscript.exe). Will cover this later
- Inside HTML page for IE Browser.
- Other host process which allows to execute code in VBScript format (ex - HP QuickTest Pro)
Internal methods and objects
VBScript provides method and objects that can be used in the script. Internal methods are one which not dependent on the Host running the script. For Ex, below code would run fine on any of scripting host
Dim myName myName = "Tarun Lalwani" Dim myNameLength myNameLength = Len(myName) |
Above code would run fine if run in standalone VBScript, HTML or QTP
Host specific methods and objects
These methods or objects are provided by the Host running the script. Scripts using such methods are specific to the Host and can only run by them. Consider the below VBScript
Dim myName myName = "Tarun Lalwani" WScript.Echo myName |
In the above script we are using a special object named “WScript” and a method “Echo” of that object. The code would run fine when run as a standalone script file and would produce the below message
No if we paste the same code in an HTML file and try to open the file in a IE browser
<html> <body> <script type="text\VBScript"> Dim myName myName = "Tarun Lalwani" WScript.Echo myName </script> </body> </html><html></html> |
The IE browser will display an error (Make sure you have unchecked the Disable Script Debugging in IE Options…Advanced Tab)
The reason the error is thrown is that WScript is Host specific object which is defined by the WScript.exe or CScript.exe scripting hosts. When we run the same code in IE this Host specific object does not exists. It is important to understand which methods or objects are host specific when porting scripts from one host to another (Ex VBScript to QTP Script, VBScript to HTML web page etc…)
Executing a Script
Different scripting host have different ways of running the script. In windows we can write the script in a notepad file and then save it with “vbs” extension
Double clicking the “Demo.vbs” file in explorer execute the script. By default windows is configured to execute the script using WScript.exe
Another way to the run the script is through windows run window or command prompt. Below screenshot shows how to execute a script using cscript.exe from console.
For an IE host we would need to run the HTML file containing the code which in turn would.
Have questions related to this article? Want to request a new article? Use our forums to post your questions
Viewed 5,082 times

(4 votes








Tags used -



































