HTA Tutorial HTA Tutorial #2

Welcome to the second tutorial. Hope you got on alright with the first. No? Send me an email if there is something you don't understand and I'll try to help. Last time, I left you with a finished program and I ended by explaining about putting comments in your code. But wait, did you update your template file so that comments can be added more easily. No? This is what it should look like:-

<html>
<HTA:APPLICATION>
<SCRIPT Language="VBScript">
<!--
' Author:-
' Date:-
' Description:-
' Comments:-
Option Explicit

-->
</SCRIPT>
<body>

</body>
</html>


This is the file you should continue to use from now on so save it as template.hta. On with some new stuff.

One of the first things you will want to do is display messages. The simplest way is to use a message box or msgbox in VBScript speak. Here it is in its simplest form:-

MsgBox "Hello"

To test this, cut and paste and place it between the script tags. Your program should look something like this:-

<html>
<HTA:APPLICATION>
<SCRIPT Language="VBScript">
<!--
' Author:- Nick
' Date:- Today
' Description:- Messages
' Comments:- A msgbox program
Option Explicit
MsgBox "Hello"
-->
</SCRIPT>
<body>

</body>
</html>

Save as Msgbox.HTA (or any name you like provide it ends in .hta) and double click on it in Windows (or Winnt) Explorer and see what it does. You must click OK to close the box. Pressiing F5 (see last tutorial) and will bring it back. Click OK and close the window in the usual way. Simple isn't it? You can display any word or words up to a limit of 255 characters provided it is written bewtween the quotes. Punctuation can also be displayed again provided it is between the quotes.

However, the MsgBox function is capable of some extra functions. From now on, I'll just be printing the code between the comments and the --> tag. I'll leave it up to you to paste the code between the tags and save it with a relevent name. Take a look at this.

Option Explicit
MsgBox "Hello World!", 64, "Hello"

This replaces the code in the previous example. It displays a message box but this time with an information icon and the box has the title "Hello" rather than just VBScript as before. There are other variations which I'll be covering in later tutorials.

Now we can communicate with the user but very often, the user needs to give the program some kind of input. This is where the InputBox function comes in. This is how it looks.

Option Explicit
DIM MyInput
MyInput = InputBox("Enter anything you like")

The DIM statement is telling VBScript that a variable containing a value is going to be used. No type is assigned because (unlike other versions of BASIC) it has only a single type called a Variant which can contain both numbers or strings or a mixture of both. This makes VBScript easier to use but can lead to bugs which can be difficult to track down particularly if a number is expected when letters have been input. A topic for another tutorial.

This code produces a box which the user can type into. It has a message telling the user what kind of input is required. Once the text has been entered, the user can either click OK (or press the Return key) to pass the value to the program and closes the box or click Cancel which simply closes the box. This example passes a value back but nothing is actually done with it. However, the following is a more useful program.

Option Explicit
Dim MyInput

MyInput = InputBox("Enter your name", "Name")
MsgBox ("Hello " & MyInput)

This time, the input box has a title and asks the user to enter his/her name. This is then passed to a message box using the variable MyInput which is then displayed along with a simple greeting. The & joins two strings together. Note the space after the word hello to make the display look better.

That completes this second tutorial. We'll get into something a little meatier next time.

Back

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