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
SetupIE()

'Main sleep loop
Do 
    'Sleep    
    WScript.Sleep 10000
Loop

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
    oIE.Navigate("about:blank")
    
    'Wait until not buisy
    Do While oIE.Busy 
        WScript.Sleep 100 
    Loop
    
    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        
    oIE.document.WriteLn(html)    
    
    'Wait until not buisy
    Do While oIE.Busy 
        WScript.Sleep 100 
    Loop    
    
    '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
    WScript.Quit
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

No comments:

Post a Comment