2

UPDATE: I figured out the answer to my first question. I used two methods, actually. For the "strBoxTitle" variable, I replaced the line:

<b>strBoxTitle</b><p>

with this

<b>
<script>document.write( strBoxTitle )</script>
</b><p>

which correctly displayed the variable's value, rather than its name. This didn’t work for the "strLocalUser" variable, however, because it was inside an input tag. For that one, I added the line

document.getElementById("userid").value = strLocalUser

to my window_OnLoad function, and changed the input tag from

<input id='userid' size=20 class='fixed' value=strLocalUser>

to just

<input id='userid' size=20 class='fixed'>

That also worked, filling in the user input field with the value of "strLocalUser", not to mention preventing me from needing to nest a script tag inside the input tag, which apparently is not allowed.

ORIGINAL QUESTION:

I'm trying to write an HTA file that gets called from a Vbscript file to get user input. The HTA file is called with the command

objWShell.Run ".\UserInput.hta | " & "Please Enter Your Credentials" & _
    " | " & strLoginID,1,True

The HTA file "UserInput.hta", which I wrote following examples here in Stack Overflow, is

<!DOCTYPE html>
<html>
<head>
    <hta:application
        id = oHTA
        border = dialog
        innerborder = no
        caption = yes
        sysmenu = no
        maximizebutton = no
        minimizebutton = no
        scroll = no
        singleinstance = no
        showintaskbar = no
        contextmenu = no
        selection = yes
    />
    <style type='text/css'>
        .fixed { font-family:courier new, monospace }
    </style>
</head>
<script language="VBScript">
    CmdLine = oHTA.commandLine
    Params = Split(CmdLine,"|")
    strBoxTitle = Params( 1 )
    strLocalUser = Params( 2 )

    Sub window_OnLoad
        Document.Title = strBoxTitle
        Window.ResizeTo 400, 200 + 70
        Window.MoveTo Window.screen.width / 2 - 200, Window.screen.height / 2 - 200
    End Sub

    Function SubmitBtn()
        Window.Close
    End Function

</script>
<body bgcolor=Silver>
    <center><form>
        <b>strBoxTitle</b><p>
        <table>
        <tr><td colspan=2 align=left>
        Please enter your username and password:<br><br>
        </td></tr><tr><td valign=top>
        Username:&nbsp;
        </td><td>
        <input id='userid' size=20 class='fixed' value=strLocalUser>
        </td></tr><tr><td valign=top>
        Password:&nbsp;
        </td><td>
        <input type='password' id='passwd' size=20 class='fixed'><p>
        </td></tr>
        </table>
        <p>
        <input type='button' value='Submit' id='but0' onclick='SubmitBtn()'>
    </form></center>
</body>
</html>

That correctly displays an input box with two input fields. Unfortunately, that's as far as I've gotten. My two questions are:

  1. How do I get the input box to display the values of the strBoxTitle and strLocalUser variables, instead of the text "strBoxTitle" and "strLocalUser"?
  2. How do I pass the user-entered values from the input boxes userid and password back to the calling Vbscript?

Thanks in advance for all guidance and suggestions.

2
  • Have you considered putting all of your VBScript code in the HTA? That's really the correct way of doing it. Otherwise, you have to do something kludgy like using the registry to pass back values. Commented Apr 26, 2022 at 20:25
  • LesFerch - Thanks for the suggestion. The problem with putting all my VBScript code in the HTA is that just getting the user name and password is really a small portion of a much larger, more complex program. It really wouldn't make sense to put everything in the HTA. On the other hand, I have no objections to kludges, so long as they work cleanly (and don't require the user to have admin privileges). If you can point me to some sample VBScript code showing how to pass values through the registry, or by writing them to a temporary file, that would be appreciated. Thanks again! Commented Apr 26, 2022 at 21:08

1 Answer 1

2

Here's your script modified to pass the result back via the registry:

UserInput.hta

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" http-equiv="X-UA-Compatible" content="IE=9">
<hta:application
id = oHTA
border = dialog
innerborder = no
caption = yes
sysmenu = no
maximizebutton = no
minimizebutton = no
scroll = no
singleinstance = no
showintaskbar = no
contextmenu = no
selection = yes
>
<script language="VBScript">
Set oWSH = CreateObject("Wscript.Shell")
CmdLine = oHTA.commandLine
Params = Split(CmdLine,"|")
strBoxTitle = Params(1)
strLocalUser = Params(2)
CenterWindow 400,270

Sub window_OnLoad
  document.Title = strBoxTitle
  title.innerHTML = strBoxTitle
  userid.Value = strLocalUser
  passwd.focus
End Sub

Sub document_onKeyDown
  If window.event.keyCode=13 Then Submit
End Sub

Sub CenterWindow(w,h)
  Window.ResizeTo w,h
  Window.MoveTo (screen.availWidth - w)/2, (screen.availHeight - h)/2
End Sub

Sub Submit
  Key = "HKCU\Software\UserInputHTA\"
  oWSH.RegWrite Key & "UserName",userid.value
  oWSH.RegWrite Key & "Password",passwd.value
  Window.Close
End Sub
</script>

<style>
.fixed { font-family:courier new, monospace }
</style>
</head>
<body bgcolor=Silver>
<center>
<b><span id=title></span></b><br><br>
Please enter your username and password:<br><br>
<table>
<tr>
<td>Username:</td>
<td><input type=text id=userid size=20 class=fixed></td>
</tr>
<tr>
<td>Password:</td>
<td><input type=password id=passwd size=20 class=fixed></td>
</tr>
</table>
<br><br>
<input type=button value=Submit id=but0 onclick=Submit()>
</center>
</body>
</html>

TestUserInput.vbs

strLoginID = "SomeUser"
strPassword = ""
Set oWSH = CreateObject("Wscript.Shell")
oWSH.Run ".\UserInput.hta " & "|Please Enter Your Credentials|" & strLoginID,1,True
Key = "HKCU\Software\UserInputHTA\"
On Error Resume Next
strLoginID = oWSH.RegRead(Key & "UserName")
strPassword = oWSH.RegRead(Key & "Password")
oWSH.RegDelete Key
On Error Goto 0
MsgBox strLoginID & vbCRLF & strPassword

Other changes: 1) Added a meta line to set the document mode to IE=9 (with only !DOCTYPE set the HTA will usually be in IE=7 mode), 2) Added a CenterWindow function that accounts for taskbar using availWidth and availHeight, 3) Set focus on the password field so the user can immediately start typing, 4) Added a check for press of Enter so user doesn't have to use mouse, 5) Eliminated the form tag which is unnecessary in this context and interferes with ability to reference elements directly by id such as passwd.value, 6) fixed issue with userid being passed with a leading space, 7) used a span for the title text (just to show another way to do it).

Other suggestion: Remove redundant requests. As written, the interface has Please Enter Your Credentials, followed by Please Enter Your Credentials, followed by Please enter your username and password.

Sign up to request clarification or add additional context in comments.

1 Comment

That is exactly what I needed, not to mention a good example of some new coding techniques. And thanks for the other improvements! I greatly appreciate your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.