VBScript Forms

by Jan Verhoeven, 12 November 2001

You can run VBScripts under the windows scripting host and use internet explorer for the graphical user interface (GUI). The code in the Code section provides a framework for building windows like application using vbscript+ie. Because the script running under the scripting host is not restricted you have full access to the file system and can create objects to access text files, databases etc.

 

Code explained

The code start with getting the folder from which the script is running. Place your html files in the same folder as the script.

Next we create an instance of internet explorer, sinking events to our script. We are only interested in the onQuit event. Whenever the ie window is closed our own sub called iexplore_onQuit will fire.

After the page is loaded we run our main loop, just to prevent the script from finishing. We use the sleep instruction to let the scripting host sleep for 100 miliseconds. Without that the loop would lockup our computer.

To allow for multipage processing I have added 2 dispatching routines: ieLoaded and ieSubmit, using a select case you can dispatch to the page related sub. ieLoaded is called after a page is completely loaded. ieSubmit is only called when you set the reference to it for an object in the loaded page, like shown for the first page:

doc.forms(0).elements("submit").onclick=getref("iesubmit")

We have shown that it is possible to use internet explorer as the graphical user interface for vbscripts. So you don't have to stick with the inputBox and the msgBox functions.

Code Section

The code below is ready to cut and paste as your own vbscript. Feel free to modify it the way you want.

'script to use InterNet Explorer as User Interface
 
dim doc, page
  fn=WScript.scriptFullName
 
p=instrRev(fn,"\")
  appldir=left(fn,p)
  set ie=WScript.createobject("internetexplorer.application","iexplore_")

  ie.visible=true

 
html="ie-form.html"
 
loadDocument(html)
  ieLoaded
  bReady=false
  do
   
WScript.sleep(100)
    if html<>"" then
     
loadDocument(html)
      ieLoaded
    end if
  loop until
bReady


  set ie=nothing
 
WScript.Quit

 
sub iexplore_onQuit()
    bReady=true
  end sub

  sub
loadDocument(adoc)
    ie.navigate(appldir & adoc)
    page=adoc
    html=""
   
'wait till document loaded
   
do while ie.readystate<>4
    Loop
    set
doc=ie.document
 
end sub

  sub
iesubmit()
    select case page
    case "ie-form.html"
     
ie_form
    case else

    end select
   
page=doc.forms(0).action
   
html=page
  end sub

  sub
ieLoaded()
    select case page
    case "ie-form.html"
     
ie_form_loaded
    case else

    end select

  end sub

  sub
ie_form()
    strName=doc.forms(0).elements("strName").value
   
msgbox(strName)
  end sub

  sub
ie_form_loaded()
    doc.forms(0).elements("submit").onclick=getref("iesubmit")
  end sub