Rem: Creating UIs with HTA

When you use VBScript’s InputBox and MsgBox functions, you’re limited in how the text boxes look. If you want to create better-looking UIs, you can use HTAs.

Michael Otey

October 7, 2004

4 Min Read
ITPro Today logo in a gray background | ITPro Today


I've written a few scripts in which I use VBScript's InputBox and MsgBox functions to get input from users and Windows Script Host's (WSH's) Echo method to display output. The scripts work well, but I'd really like to have a better way to obtain input and display output. How can I provide better-looking UIs in my scripts?

There are several ways to provide better-looking UIs. One of the most common ways is to use HTML Applications (HTAs). Microsoft introduced HTAs in Microsoft Internet Explorer (IE) 5.0. At their core, HTAs are Web pages. They contain HTLM, Cascading Style Sheets (CSS), and embedded scripting code. However, unlike standard HTML Web pages, HTAs aren't run from a server. Instead, they're run from the desktop. Another difference is that standard HTML Web pages use the .htm extension, whereas HTAs use the .hta extension.

In addition to being a useful front end for administrative scripts, HTAs are commonly used for setup programs. In its simplest form, an HTA can contain pure HTML code, but that would result in a static page. Because HTAs can support embedded scripting code, you can use them dynamically to address just about any scripting task.

Much like standard Web pages, HTAs contain HTML tags. HTAs begin and end with the and tags, respectively. However, HTAs have some unique attributes that set them apart from HTML pages, including the tag and the tags. The tag designates the page as an HTA. The tags mark the beginning and end of a section of scripting code, much like in Active Server Pages (ASP). HTAs support both embedded VBScript and JScript code.

Listing 2 shows a sample HTA called ReadFile.hta. This HTA prompts users to input a filename, then click the Read File button. After users click that button, the HTA executes VBScript code that reads the filename, opens the file, and displays the file's contents on the page.

Let's take a closer look at how ReadFile.hta works. ReadFile.hta begins with the standard tag to denote the beginning of the document. The tag contains the output for the document's title bar.

The code at callout A in Listing 2 is the core of the application. The attributes following the tag define how the HTA window will appear. For example, the attributes specify the window's border style and width.

Next, you'll find the Callout B shows the HTML code that creates the UI. First, the code sets the background to the color gray, then it creates a simple table that contains two UI objects: a text box in which users input a filename and a button that, when clicked, executes the VBScript code. Setting the tag's Type attribute to "text" creates the text box. The Size attribute sets the text box's width to 50 characters. The Name attribute assigns the text box the name fName, which corresponds to the fName object in the VBScript code. The tag creates the button. The Style attribute sets the button's appearance. The onClick attribute specifies that the ReadFile subroutine will be executed when the user clicks the button.The VBScript code between the tags contains the ReadFile subroutine. First, this subroutine creates a FileSystemObject object named oFS, then uses that object's FileExists method to test for the existence of the fName.value file. (VBScript views the UI&apos;s text box as an object&#x2014;i.e., the fName object&#x2014;and this object&apos;s Value property contains the filename.) If the FileExists method doesn&apos;t find the specified file, the subroutine displays a message that states the file wasn&apos;t found. Otherwise, the subroutine uses the oFS object&apos;s OpenTextFile method to create a TextStream object named oFile. The subroutine uses the oFile object&apos;s ReadAll method to read the contents of the file into the sContents variable and assigns this variable to the HTA&apos;s document.body.InnerHTML property, which displays the contents of the file in the HTA window.</p><p>Although ReadFile.hta displays the file&apos;s contents in a window that looks very much like the standard IE window, HTAs aren&apos;t limited to that look. You have a great deal of control over what the UI looks like. Moreover, you can execute just about any VBScript code from within the HTA. You can find more information about scripting and HTA applications at http://msdn.microsoft.com/library/default.asp?url=/workshop/author/hta/overview/htaoverview.asp.</p>

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like