Search This Blog

Wednesday, April 24, 2013

VBScript Internet Explorer UI made simple.

I needed a user interface for a deployment script I was writing in visual basic script.  There were many examples on how to make an IE UI with VBS but, I could not find one that was event driven.

So I have came up with a simple way to do a UI for VBS.  I hope this helps you.
The trick is using the TitleChange event in IE to get user input.

Option Explicit

Dim oIE
Dim oShell

'Setup IE options

'Main sleep loop
    WScript.Sleep 10000

Sub SetupIE()
    Dim html

    'Initalzie IE and setup attributes    
    Set oIE = WScript.CreateObject("InternetExplorer.Application", "oIE_")
    With oIE    
        .ToolBar   = False 
        .RegisterAsDropTarget = False
        .StatusBar = False 
        .width = 600
        .height = 600
        .top = 1
        .left = 1
    End With
    'Clear the screen
    'Wait until not buisy
    Do While oIE.Busy 
        WScript.Sleep 100 
    html = "<html>" & vbCrLf _
        &"<header><title>My Title</title></header>" & vbCrLf _
        &"<body scroll=""no"">" & vbCrLf _
        &"<script>" & vbCrLf _
        &"command=""""" & vbCrLf _
        &"function RunCommand(cmd) {" & vbCrLf _        
        &"command = cmd;" & vbCrLf _
        &"document.title = document.title;" & vbCrLf _
        &"}" & vbCrLf _
        &"</script>" & vbCrLf _
        &"<input id=""inputTest"">" & vbCrLf _ 
        &"<button onclick='RunCommand(""go"")'>GO</button>" & vbCrLf _
        &"<div id=""output""></div>" & vbCrLf _
        &"</body>" & vbCrLf
    'Draw html on window        
    'Wait until not buisy
    Do While oIE.Busy 
        WScript.Sleep 100 
    'Show the window
    oIE.Visible = true    
    'Create the shell object
    Set oShell = CreateObject("WScript.Shell")
    'Force to front
    oShell.AppActivate oIE    
End Sub

' Execute code when IE closes
Sub oIE_onQuit       
    'Close the script
End Sub

Sub oIE_TitleChange(text)    
    'Check for program page loaded
    If text <> "about:blank" and text <> "http:///" Then            
        'Show the command
        oIE.document.ParentWindow.output.innerHTML = oIE.document.ParentWindow.command & "<br>" & oIE.document.ParentWindow.inputTest.Value
        'Clear the command - optional
        oIE.document.ParentWindow.command = ""        
    End If
End Sub