VBScript Tutorial

VBScript Tutorial #1

To follow this tutorial, you will need access to a Windows PC running Internet Explorer 5.0 or above. It will be necessary to construct a basic template file to make creating new files so much easier. This file will look something like this:-

<html>
<SCRIPT Language="VBScript">
<!--
Option Explicit
-->
</SCRIPT>
<body>

</body>
</html>

You will notice that it is an HTML file but embedded within it is a program written in script (in this case Visual Basic Script). It will only run in IE 5.xx or above and will be ignored by other browsers. Why use VB? Because it is easy to learn especially if you are used to the BASIC programming language. I'll go through the file a line at a time.

<html>

It's an ordinary web page which always starts with this tag to tell the browser to treat it as command rather than just plain text.

<SCRIPT Language="VBScript">

Another important tag. Without this, the browser will interpret all the code as simple text rather than as a program.

<!--

Not strictly necessary but useful if you want to run it in a proper browser window. Everything after this and before the next tag will be interpreted as program source code and will produce an error if it isn't. More on that later.

Option Explicit

Again, not strictly required but it will produce an error if a variable has not been declared which I consider to be good programming practice. Leave it out if you're not bothered (although don't blame me if you get some funny bugs).

-->

End of the script.

</SCRIPT>

Close the script section.

<body>

Now the HTML proper can be specified. More later.

</body>
</html>

Close the main body of the HTML and close the file itself. Here's that file in full again.

<html>
<SCRIPT Language="VBScript">
<!--
Option Explicit
-->
</SCRIPT>
<body>

</body>
</html>

Cut and paste this into a file and name it Template.HTM. Double click on it in Windows Explorer and wait for IE to load and check there are no errors and that the browser window is blank. If there is writing of some kind then you have made an error somewhere. Now make a copy of template.htm and rename it to Msgbox.HTM. Between the two code tags add:-

msgbox "Hello"

This will make the file look as follows:-

<html>
<SCRIPT Language="VBScript">
<!--
Option Explicit
msgbox "Hello"
-->
</SCRIPT>
<body>

</body>
</html>


Save it and run it as before. You should see a standard Windows message box containing a greeting. Click OK. Note that the browser window remains. Pressing F5 will refresh it and run the program again. Click OK and close the browser in the normal way. Congratulations. You have written your first script.

One thing needs to be added for completeness and that is some comments about the program, who wrote it, the date and perhaps a description of what it does. Probably not required on such a small program as this but it is a good habit to get into. Take a look at this:-

<SCRIPT Language="VBScript">
<!--
' Author:- Nick
' Date:- Now
' Description:- Hello program
' Comments:- Pretty simple eh?
Option Explicit
REM This is a comment
MsgBox ("This is code")
' This is also a comment
-->
</SCRIPT>

I have ommitted all the HTML stuff for clarity but it will need to be included when the file is created. There are several ways of showing comments one of which is the REM or Reminder statement. Really a hangover from previous versions of BASIC and not really used anymore. The other is an apostrophe as shown above. Comments are part of the code and so must be inside the script tags. They can also be added between lines of code to explain what is happening although excessive use of comments can slow the code down a bit - not a problem on new machines but could give one of your users a problem if they are using older kit. The program is now complete.

If you want to know more then view the next tutorial from the main menu.

Back

Written by Nick Cheesman. Last updated: 01/04/2004
Please eMail me at:
nickjc@nickjc.co.uk