Show a modal Dialog using MS IE

The following script demonstrates how to show a modal dialog box using the Microsoft Internet Explorer. Within the dialog box a HTML-document (with a user defined content) may be shown. This is an excellent feature to display something from a WSH script without using the methods Echo or Popup or the VBScript MsgBox function.

'************************************************
' File: WSHDialog.vbs (WSH-sample in VBScript)
' Author: (c) G. Born 
'
' The script demonstrates how to use MS IE 4.0 to display
' a HTML-document in a modal dialog from WSH.
'
' In no way shall the author be liable for any
' losses or damages resulting from the use of this
' program. Use AS-IS at your own risk.
'
' The code is the property of the author. You may
' use the code and modify it, as far as this header
' remains intact. Further updates and other samples
' may be found on my site:
' http://www.borncity.de
'************************************************
Option Explicit

Dim WSHShell ' set variables
Dim oIE4
Dim path
Dim Title

' *** get the path to the script file, because we
' *** need it to load the HTML-file within the dialog
path = WScript.ScriptFullName
path = Left(path, InstrRev(path, "\"))

' *** launch Internet Explorer ***
Set oIE4 = WScript.CreateObject("InternetExplorer.Application")
oIE4.navigate "about:blank" ' empty document
' MS IE windows is invisible, we need only the dialog

' Important: Waite till MSIE is ready
Do While (oIE4.Busy) 
Loop
' show the modal dialog using ShowModalDialog
'
' Tip: the form in test1.htm contains a Close button
' Using showModalDialog causes the WSH to wait till
' the dialog is closed.
'
' Attention: During tests with MS IE 5.0 (Beta) I found a problem,
' if you close the dialog using the button, MS IE 5.0 reopens
' the dialog again in a second window. IE 4.0 works properly.

oIE4.Document.Script.showModalDialog path & "test1.htm",,_
"dialogHeight: 15" 

' This messages will be shown after closing the dialog
WScript.Echo "Dialog closed "

oIE4.Quit() ' close MS IE
Set oIE4 = Nothing ' release object variable

'*** End

The VBScript command to launch Internet Explorer must be written as:

Set oIE4 = WScript.CreateObject("InternetExplorer.Application")

Then we try to force the Internet Explorer to load an empty document (because we like to use a modal dialog to show our HTML file). In MS IE 4.0/5.0 we can do this by the following simple statement:

oIE4.navigate "about:blank" ' empty document

And I use another trick: I keep the MS IE windows invisible, because we need only the dialog. There is also an important issue, I like to mention. Before we may continue to tamper with the MS IE, we must wait till the MS IE is ready (otherwise we get sporadic run-time errors in the script, caused by the inability of the browser to accept calls from the script!). I have used in my first attempts delay loops (but a loop depends on the processor speed). Finally I found a much more simple and reliable solution. The following loop checks the Busy state of the MS IE and delays further processing, till the browser has loaded the blank document properly:

Do While (oIE4.Busy) 
Loop

Now we are ready to show the modal dialog using ShowModalDialog of the browser. The modal dialog box is invoked using the following command:

oIE4.Document.Script.showModalDialog path + "test1.htm",, _
"dialogHeight: 15" 

I intend to show the content of the file test1.htm, which must be located in the same folder as our WSH script. The name of the script's path is contained in the variable path. I set the value of this variable in the header of our script (using some functions to retrieve the path to the script).

Tip: The form in test1.htm contains a Close button. Using showModalDialog causes the WSH to wait till the dialog is closed.

Now we are ready to have a look at our HTML-document, which shall be shown in the modal dialog. The file test1.htm contains just a simple text and a Close button (for demonstration purposes). Below is the HTML-source code to show the form:

<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="Microsoft FrontPage Express 2.0">
<title>Born's Demo</title>
</head>
<body bgcolor="#FFFFFF" bgproperties="fixed">
<h2 align="center">Born's Dialog-Demo</h2>
<p align="center"><strong>Where you want to go today?</strong></p>
<p align="center"><font size="2">by Günter Born</font></p>
<form method="POST">
<p align="center"><input type="submit" name="B1"
value="Close" onclick="window.close()"></p>
</form>
</body>

Well, that's all. We can use this script to show any information (about boxes, status messages and more). Additional information about this sample and the way how to create a GUI for WSH scripts (including real forms - yes, it is possible) may be found in my German WSH book Inside Windows Script Host, published by Microsoft Press. Also my Microsoft Press Title Microsoft Windows Script Host 2.0 Developer's Guide contains a discussion about IE forms.

Back