An HTA based VBScript UserForm

One of the shortcomings of VBScript is its lack of a Userform. This sample provides an approach to building VBScript based userforms using of the HTA interface to IE's DHTML DOM, without encountering its security restrictions.

4 Star
Add to favorites
E-mail Twitter Digg Facebook
Sign in to ask a question

  • VBScript: Error: permission denied: 800A0046
    3 Posts | Last post August 01, 2018
    • Hello Tom,
      I want to use your script within a SCCM package to install software.
      When I execute the script as normal user on my PC it works fine.
      When I use PSTools to simulate the execution of the script as SCCM instance I get the 800A0046 error (permission denied) on line 45 ("next") in the script.
      Any ideas what causes this and how to change the code?
      Thanks and best regards
    • Benedikt,
      I am getting the same error code.
      - I believe that it is caused by line 31 "    .Run sCmd, 1, False " {i.e. when mshta.exe is run}  [[Adding a comment line before this line changes the reported line number in the error dialog, adding a comment line after it does not.]]
      - It is error code 800A0046 (permission denied)
      - I get the error every time & for all users [Admin & Std]
      - Starting on 24th July 2018, Windows Defender also reports this as a Trojan.  I have not been able to fix this by adding exclusions for: the batch file calling the vbs, the vbs, mshta.exe itself.
      - I think the problem started at some stage during Windows 10 Ver 1709; it continues with Ver 1803.  I cannot be sure when it started because I use it within error reporting so I did not realise there was a problem with this until there was a problem with something else that had to be reported.
      Since this script uses hta to access IE & given that IE is now only patched for security vulnerabilities [not functional issues], perhaps the script has now become incompatible with Windows 10 & has therefore reached its end of life.
    • I have been able to 'allow the threat' of this false positive Windows defender detection and can now use the script without errors once again.
      I do not know why I was unable to use WD's Exclusions list instead but the 'Allow the threat' procedure lets me carry on using the script with both Ver 1709 & Ver 1803.  This procedure has to be repeated on each computer.
  • How to keep the HTAbox always on top?
    1 Posts | Last post February 20, 2018
    • Many thanks to Tom for the script provided. It actually opened a door to call HTA from VBscript.
      I want to keep the HTAbox always on top of all other windows. Once other apps cover this window, I would like it can popup to the top once in a while.
      I tried to use .document.focus() function in the loop, but it only tops once and then the HTAbox is covered by other apps.
      How to keep HTAbox always on top?
      Appreciate your help!
  • Can sMessage be left-aligned?
    5 Posts | Last post September 23, 2016
    • I am using your function and am very grateful for your posting the script.
      Can the parameter sMessage be left-aligned on the screen?  I have tried inserting ; text-align:left and a few variations of this in front of ;font:normal but cannot get it to change its center alignment.
      I am particularly keen to control the alignment because I want to use the script for multiple line sMessages and they just look daft if they are all aligned on the center.  Adding the multiple lines works okay, it is just the alignment that is foxing me.
    • The problem you are having is due to a set of <center> tags I used in the original definition of the HTABox subroutine.  Just remove them from the last line of the HTABox.document.write statement.  Specifically look for this line:
                   & "<center><span id=msg>&nbsp;</span><center></body>" 
      and change it to this:
                   & "<span id=msg>&nbsp;</span></body>" 
      Oops, I just noticed an error in the original formulation (which does not affect your problem, at all).  The second <center> HTML tag should have a forward slash, as in </center> to turn off the centering for text that follows it.
    • Tom,
      Thanks.  And now I feel stupid.  I thought I had tried that already but must have made an error in doing so as it works just as you said.
      I can now control text layout purely through the entries in the sMessage & sControl statements.  {I have chosen to center all buttons & the sMessage top line but with all subsequent sMessage lines left-aligned}
      I have made a customisable, reusable tool out of your function.  The tool is called by a batch file that passes the necessary arguments that achieve customisation on-the-fly [the vbs containing the HTA function does not change].  
      The current version is limited to buttons but I have structured it in such a way that it will be amenable to adding other elements later such as input boxes, checkboxes or radio selections {I expect that having one of these extra element types could be okay visually but having more than one will exhaust the possibilities of customisation on-the-fly as some design effort would be needed to achieve a decent layout].
    • Tom,
      In case you are interested, I have posted a couple of sample outputs in the MSA forum at 
      In each case, the caller [batch file] defined the required customisations whilst the vbs file containing your function remained unchanged.  In practice, the contents of the caller [batch file] are pasted into my operational batch files as a SubRoutine for use when user interaction is required.  I had previously been limited to calling MsgBoxes, PopUpBoxes & InputBoxes and coping with their limitations.
      I expect to finish amendments & testing next week.  I will post the final versions in the same MSA thread.  Posting here as a forum page does not seem appropriate since what I have done is entirely derivative but I can add the links to the final versions here if you think that would be helpful.  
    • As I said in my introduction, the function was intended as a canvas onto which a user could write any control they wanted using the set of Dynamic HTML statements. The real focal point of the exercise was to show how to get a handle to the HTA window so that it could be manipulated programmatically. If you look in the VBS example, I added a couple of controls to the message box (an input box and a button). I fully expected a user to expand on those examples to serve there personal requirements.  So, I'm happy you are able to exploit it to serve your needs.
  • How to get an HTML variable in VBScript
    3 Posts | Last post February 19, 2016
    • Hi Tom - The script is doing very well but how do you get back in VBS the input variables which have been typed in HTML ?
      More precisely, if I replace the first line of the script by :
      sMessage =  _ 
      "Name" & "<INPUT NAME=" & g & "Xname" & g & " SIZE=" & g & "20" & g & "><BR>"& _ 
      "<font color=red><b>This is your important message</b></font>" 
      if "John" is typed as an html input for Xname, we should eventually see "John" with the following final instruction :
       wsh.echo "User pressed button" & chr(13) & " Xname= ? " & xname 
      But it does not work, the variable remains desperately empty. What is wrong ? Thanks
    • Here, try this:
      sMessage =  _ 
        "Name" & "<INPUT type=text NAME=Xname SIZE=20><BR>"& _ 
        "<font color=red><b>This is your important message</b></font>" 
      sControl = "<input type=submit id=Btn1 value=OK onclick='done.value=""clicked""'>" 
      sHTMLCode = sMessage & "<hr>" & sControl 
      with HTABox("lightgrey", 150, 300, 400, 500) 
        .document.title = "My Message Box" 
        .msg.innerHTML = sHTMLCode 
        Timeout = 10000 ' milliseconds 
        do until .done.value = "clicked" or (n > TimeOut): wsh.sleep 50 : n=n+50 : loop 
        if .done.value = "clicked" then  
          wsh.echo "User typed:" , .XName.value
          wsh.echo "Timed out" 
        end if 
        .done.value = true 
      end with 
      Note that I changed the button to a SUBMIT.  In that case, pressing Enter is equivalent to pressing the OK button.  I also put the focus on the input box using the .XName.focus line.
      Tom Lavedas
    • It works perfectly and really enhances the scope of application of VBS, thank you very much Tom.
  • Minimize and maximize button
    2 Posts | Last post February 21, 2015
    • Tom,
      I was wondering how to enable the features under HTABox.document.write
      I would like to be able to minimize the box. I tried changing the no to yes on the "minimizebutton" but that doesn't seem to work.
      Thank you for the script
    • It would appear you need to also enable the sysmenu item to get that to work.
      That is ...
            HTABox.document.write _ 
                     "<HTA:Application contextMenu=no border=thin " _ 
                   & "minimizebutton=yes maximizebutton=no sysmenu=yes />" _ 
      etc ...
      That should do it.
  • No question just a thanks
    1 Posts | Last post May 22, 2013
    • Thanks for your donation of this script, it was a time saver.
  • More buttons
    5 Posts | Last post March 19, 2013
    • Hi,
      Thanks for your script, it's being very useful for me. However, I need two buttons, OK and Cancel, if pressed OK the script has to do something, close de window and return to the point where it was called and if pressed cancel has to return without doing that something, and I can't get it to work. 
      May yo help me, please?
    • That's easily done in the main script. Here's an example, ...
      sMessage = "<font color=red><b>This is your important message</b></font>"
      sControl = "<input type=button id=Btn1 value=' OK ' onclick='done.value=""clickedOK""'> " _
               & "<input type=button id=Btn2 value=Cancel onclick='done.value=""Canceled""'>"
      sHTMLCode = sMessage & "<hr>" & sControl
      with HTABox("lightgrey", 125, 300, 400, 500)
        .document.title = "My Message Box"
        .msg.innerHTML = sHTMLCode
        Timeout = 3000 ' milliseconds
        do until .done.value <> "false" or (n > TimeOut): wsh.sleep 50 : n=n+50 : loop
        if .done.value = "clickedOK" then 
          wsh.echo "User pressed OK button"
        elseif .done.value = "Canceled" then 
          wsh.echo "User pressed Cancel button"
        elseif n > TimeOut then
          wsh.echo "Timed out"
          wsh.echo "Problem", .done.value
        end if
        .done.value = true
      end with
      To remove the timeout, change this line ...
        do until .done.value <> "false" or (n > TimeOut): wsh.sleep 50 : n=n+50 : loop
      to this ...
        do until .done.value <> "false" : wsh.sleep 50 : loop
      Tom Lavedas
    • Thank you very much, Tom. Your script has save me :-) I don't know how to thank you. I have been working with it, and I have learn very much studying it. But I have an strange problem with it: if I use it in an vbs in Windows 7 or XP, it works fine. But if I run the same vbs in a Windows 2003 or 2008 (any version), it works slowly. For example, a very simple application, that shows two buttons, Ok and Cancel, in a Windows 7 or XP, it takes less than one second to show the windown and both buttons. In server versions of Windows, it takes about one or two seconds draw the window, and another 2 or 3 seconds to show both buttons.
      But, if I make an hta file with this code:
      <title>HTA Test</title>
      <script language="VBScript">
          Sub TestSub
              Msgbox "Testing 1-2-3."
          End Sub
      <input type="button" value="Run Script" name="run_button"  onClick="TestSub"><p> 
      The window and button appears almost instantly, so I guess the delay is a problem when creating the MSHTA and IE instance. Can you help me to solve this problem? I'm not a developer, and I need help. 
      Thank you very much.
    • I'm sorry, but I don't have access to either of the server OS versions you are having problems with, so I can't address your problem.  Note that the script was not verified on any OS beyond Win 7, though I do suggest it should work.  In fact, it does work, just with the problems of timing that you report.  It might be a result of an anti-virus scan, but I have no way of investigating that.  It might instead be caused by the need to load IE as a means of passing the HTA window object back to the script.  It might also have something to do with the size or speed if the paging cache on the server machine, but again, I have no way of testing any of these possibilities, sorry.
    • Ok, thank you, Tom.
  • Script Error
    3 Posts | Last post October 17, 2012
    • Hi Guys I have tried this script on  a Windows 7 system but it doesn't work.
      Receive an error: "Line 2 Char: 15 - Expected ';'
      I have tried some modification without sucess.
      Apreciate if someone have any solution that can be shared.
    • I used the COPY function from the Repository and pasted the resulting copy into Notepad and saved it as a VBS file to check this report.  The script ran as expected without the reported error on my Win7 Enterprise Edition equipped machine.  So, I don't know what the problem might be with your attempt.
      The need for a semicolon suggests that something is being interpreted as JScript, rather than as VBS, but I don't know what that might be.
    • Hi Ton,
      I have tried on a Windows 2003 machine and it worked as expected. Really weird the behavior on my machine.
      Thnak you!