If you’ve been following my series of Daily Drill Downs on using Windows Script Host and VBScript, you know the lack of a feature for creating customizable dialog boxes limits your ability to create nice-looking interactive scripts. For example, VBScript has an Input dialog box with which you can prompt the user to type information in a text box and a Message dialog box with which you can display buttons and prompt the user to make a selection by clicking one of them. Regrettably, this means that there is no native way to implement such traditional dialog box controls as text boxes, radio buttons, check boxes, drop-down list boxes, or scrolling text boxes.

In ”Automating IPConfig in Windows NT/2000 using Windows Scripting Host,” I used an HTML document with form elements to create a dialog box and then called that file from another HTML document using the JScript showModalDialog feature. While this technique did the trick, it was cumbersome and didn’t provide an easily reusable solution.

I’ve recently discovered a better way to create custom dialog boxes in Windows Script Host by automating Internet Explorer (IE). With this technique, you still create the actual dialog box in HTML as a form, but you access the HTML document by automating the IE object from within VBScript, rather than just launching IE and loading the HTML file. This gives you more control over IE and subsequently the dialog box.

Since having a tool to create custom dialog boxes in VBScript would be a tremendous asset for creating scripts that prompt users to make decisions, I decided to implement this script as a reusable stand-alone function appropriately named DialogBox. I developed a Windows Script File that accesses the DialogBox function, allowing you to quickly and easily create customized dialog boxes. In this Daily Drill Down, I’ll show you how to create the reusable DialogBox function.

Before we get started with the scripts, let’s take a moment for a brief overview of the technique. The stand-alone DialogBox function is designed to work as an intermediary between two other files. The first file is a Windows Script File, which will incorporate the stand-alone DialogBox function as an include file. The script in the Windows Script File will then be able to access the DialogBox function as if it were a native VBScript function.

Just like many native VBScript functions, the DialogBox function requires a set of parameters to be sent to it. These parameters will essentially tell the DialogBox function how big to make the dialog box and what to put in it. The information about what to put in the dialog box will come from an HTML document, whose name will be passed to the DialogBox function as one of the parameters.

The DialogBox function
Now that you have a general idea of technique, let’s take a look at the central element of this technique—the DialogBox function. To begin, the DialogBox function is saved in a file called FDialogBox.vbs, as shown in Listing A.

To better describe how the DialogBox function works, I’ll break it down into its four key sections—the set of parameters, the dialog box shell, error handling, and input detection.

Listing A: FDialogBox.vbs

  1. Function DialogBox(L, T, H, W, Cancel, File)
  2. Dim DFile
  3. Set DFile = FileSystem.GetFile(File)
  4. IEDbx.Left = L
  5. IEDbx.Top = T
  6. IEDbx.Height = H
  7. IEDbx.Width = W
  8. IEDbx.MenuBar = 0
  9. IEDbx.ToolBar = 0
  10. IEDbx.StatusBar = 0
  11. IEDbx.navigate DFile
  12. IEDbx.Visible = 1
  13. Do While (IEDbx.Busy)
  14. WScript.Sleep 200
  15. Loop
  16. On Error Resume Next
  17. Do
  18. WScript.Sleep 200
  19. Loop While (IEDbx.Document.Script.ClickButton() = 0)
  20. If IEDbx.Document.Script.ClickButton() = 3 Then
  21. Cancel = True
  22. End If
  23. If Err <> 0 Then
  24. WScript.Quit
  25. End If
  26. On Error GoTo 0
  27. End Function

The base parameters
To use the dialog box function, you’ll need to pass it six parameters. The first two parameters contain numbers that specify the screen coordinates for the top left corner of the dialog box. The third and forth parameters, also numbers, specify the height and width of the dialog box, respectively.

The fifth parameter is a flag that will be used to determine whether the Cancel button is clicked. This flag is set to False by the calling routine, which in this case is a Windows Script File. Finally, the sixth parameter is the name of the HTML file that contains the contents of the dialog box. You can see how each of these parameters comes into the DialogBox function in line 1.

Lines 2 and 3 play a small, yet key role in the DialogBox function—they determine the full path to the HTML file no matter where it is on the hard disk. In line 2, I set up a variable called DFile. Then, in line 3, I use the GetFile method from the FileSystemObject and pass it the name of the HTML file. This method will return the full path to the HTML file and then store it in the DFile variable.

The dialog box shell
The dialog box is actually created by automating IE and manipulating components of its object model. As you can see, lines 4 through 12 use an object variable called IEDbx, which is actually initiated in the calling routine, and a group of properties and methods that the IE application object makes available. Keep in mind that for the DialogBox function to work properly, the IE object must be named IEDbx.

As you can see, the methods in lines 4 through 7 use the values stored in the first four parameters to establish the dimensions for the dialog box. Next, lines 8, 9, and 10 disable IE’s menu bar, toolbar, and status bar, respectively, making IE look more like a traditional dialog box.

Line 11 uses the navigate method to load the HTML file, whose full path is contained in the DFile variable. Then, line 12 sets the Visible property to one, which displays the browser window.

Moving on, lines 13 through 15 make up a small Do…Loop that continuously checks the status of IE’s Busy property as it builds the dialog box. Inside the loop, the script uses the Sleep method to pause the script for 0.2 second. The loop will continue until IE sets the Busy property to False, which indicates that it is finished loading the HTML file and building the dialog box.

Error handling
By default, the dialog box will contain OK and Cancel buttons that are used to signal either the completion or dismissal of the dialog box. There are two other ways the dialog box can be closed. You can click the Close button in the upper-right corner of the window or select the Close command from the Command menu in the upper-left corner of the window. Closing the dialog box using either of these methods would cause Windows Script Host to generate a run-time error—a situation that will display an error message and halt the script. To avoid this situation, line 16 initiates the built-in VBScript error handler—the On Error Resume Next statement.

With this error handler in place, if the window is closed by either of those methods and an error occurs, the script jumps to the statement immediately following the statement in which the error was generated. In this case, line 23 takes over and tests the value in the Err object, which is set to some number other than zero by the generator of the error. Since the test will be true, I then use the Windows Script Host Quit method to quickly terminate the script.

Line 26 comes into play when the DialogBox function works correctly and simply disables the error handling right before the function ends.

Detecting input
Lines 17 through 19 implement a Do…Loop that simply pauses the script and waits for the user to click a button in the dialog box. Once a button is clicked, the ClickButton function (a small Public function contained in the script section of the HTML file) returns a value based on which button was clicked, and the Do…Loop terminates.

In lines 20 through 22, I use an If…Then statement to determine whether the value returned by the ClickButton function is three. If it is, the user clicked the Cancel button. I then set the Cancel flag to True. (We’ll see how the Cancel flag comes into play in my follow-up Daily Drill Down when we examine the script in the Windows Script File.)

If the value returned by the ClickButton function isn’t three, the script assumes that the user clicked OK. The DialogBox function then ends and returns control to the calling routine.

With the information in this Daily Drill Down, you can now create a reusable dialog box function for Windows Script Host. By showing you how to automate the IE object from within VBScript, I’ve given you some insight into the inner workings of this type of handy, reusable script. My goal with this Daily Drill Down is to fill a void in Windows Script Host with regard to creating customizable dialog boxes. In my next Daily Drill Down, I will teach you how to build the HTML content document and the Windows Script File Drive template and show you how the three files work together to implement my reusable dialog box technique.