1

The example script was originally posted by OMEGASTRIPES here: https://pastebin.com/wZYTtT8V

and was further discussed here, on Stack Overflow, under the following link: Pass two lines of text into InputBox

The script does basically exactly what I need (or at least the first part of it), which is to capture a long string of text (including a line break), which I then need to massage, in order to re-use parts of the string. The script is now doing everything I require with one exception. The example script presents the user's input in a window at the end. Instead of presenting the window, I merely want to assign the value of the user's input to a variable. Not being a developer (just dipping my toe occasionally, as and when necessary) I have struggled for several hours in trying to achieve this, including failed attempts at trying to use GetElementbyName. Could someone please put me out of my misery, as I'm sure this is going to turn out to be really simple, but I just don't know how and my research is drawing a blank. The offending lines in the example script are in the 3rd area of the case statement:

inputboxml = window.hta_textarea.value
window.close

The following is just one example of many different approaches I tried (and failed) to assign the value of the input to a variable:

inputboxml = document.getElementsByName("hta_textarea")

Any help anyone could offer me would be most gratefully received. Below is the entire example script.

Thanks, Steve

'Example Script

dim completed
 
msgbox inputboxml("Enter text:", "Multiline inputbox via HTA", "default" & vbcrlf & vbtab & "multiline" & vbcrlf & "text")
 
function inputboxml(prompt, title, defval)
    set window = createwindow()
    completed = 0
    defval = replace(replace(replace(defval, "&", "&amp;"), "<", "&lt;"), ">", "&gt;")
    with window
        with .document
            .title = title
            .body.style.background = "buttonface"
            .body.style.fontfamily = "consolas, courier new"
            .body.style.fontsize = "8pt"
            .body.innerhtml = "<div><center><nobr>" & prompt & "</nobr><br><br></center><textarea id='hta_textarea' style='font-family: consolas, courier new; width: 100%; height: 580px;'>" & defval & "</textarea><br><button id='hta_cancel' style='font-family: consolas, courier new; width: 85px; margin: 10px; padding: 3px; float: right;'>Cancel</button><button id='hta_ok' style='font-family: consolas, courier new; width: 85px; margin: 10px; padding: 3px; float: right;'>OK</button></div>"
        end with
        .resizeto 700, 700
        .moveto 100, 100
    end with
    window.hta_textarea.focus
    set window.hta_cancel.onclick = getref("hta_cancel")
    set window.hta_ok.onclick = getref("hta_ok")
    set window.document.body.onunload = getref("hta_onunload")
    do until completed > 0
        wscript.sleep 10
    loop
    select case completed
    case 1
        inputboxml = ""
    case 2
        inputboxml = ""
        window.close
    case 3
        inputboxml = window.hta_textarea.value
        window.close
    end select
end function
 
function createwindow()
    rem source http://forum.script-coding.com/viewtopic.php?pid=75356#p75356
    dim signature, shellwnd, proc
    on error resume next
    signature = left(createobject("Scriptlet.TypeLib").guid, 38)
    do
        set proc = createobject("WScript.Shell").exec("mshta ""about:<head><script>moveTo(-32000,-32000);</script><hta:application id=app border=dialog minimizebutton=no maximizebutton=no scroll=no showintaskbar=yes contextmenu=no selection=yes innerborder=no icon=""%windir%\system32\notepad.exe""/><object id='shellwindow' classid='clsid:8856F961-340A-11D0-A96B-00C04FD705A2'><param name=RegisterAsBrowser value=1></object><script>shellwindow.putproperty('" & signature & "',document.parentWindow);</script></head>""")
        do
            if proc.status > 0 then exit do
            for each shellwnd in createobject("Shell.Application").windows
                set createwindow = shellwnd.getproperty(signature)
                if err.number = 0 then exit function
                err.clear
            next
        loop
    loop
end function
 
sub hta_onunload
    completed = 1
end sub
 
sub hta_cancel
    completed = 2
end sub
 
sub hta_ok
    completed = 3
end sub

1 Answers1

1

I think you can just set a variable to the output of the inputboxml function instead of sending that output direct to a msgbox.

' instead of this
'msgbox inputboxml("Enter text:", "Multiline inputbox via HTA", "default" & vbcrlf & vbtab & "multiline" & vbcrlf & "text")

' use this
dVal = inputboxml("Enter text:", "Multiline inputbox via HTA", "default" & vbcrlf & vbtab & "multiline" & vbcrlf & "text")
 
msgbox dVal

I've added a msgbox to just check the results are the same.

Doofus
  • 11
  • 1
  • Hi Doofus, many thanks for the suggestion. However, unless I made a silly mistake (which I don't think I did), I don't think what you have suggested works! As well as setting dVal at the start, I replaced the two lines in the 'Case 3' area with the message box and it comes up blank! Please forgive me for asking, but did you test your answer using the example code I provided and, if so, did it work on your machine? Kind regards Steve – Steve Tomalin Jul 03 '21 at 20:48
  • Hi Steve. I did test using your code and it did work. I was aiming to amend what you had as minimally as possible. What I didn't do was Dim the dVal variable before using it, so it isn't available to any of the Sub routines. Add a `Dim dVal` before the `dVal =` line. Compare with the `Completed` variable, which is `Dim`'d at the start and which is available in the Sub's. – Doofus Jul 05 '21 at 07:50
  • Steve, my apologies, you said in your comment you were trying to access the dVal variable from the `Select Case` within the function. I mis-read that the first time, thinking you were referring to the `Sub hta_ok`. I don`t think you will be able to access the dVal while still within the function as its will be set based on what the function returns. You need to put back the original two lines in the Case 3, they will set the output of the function and close the HTA window so are important. Put your msgbox in the hta_ok sub. – Doofus Jul 05 '21 at 14:36
  • Hi Doofus, Haven't been here for a while. In the end I found an example that did what I needed in PowerShell, but it would be better if I could get it working in a VB script. I will give what you have suggested a try, as a recent change to my PC environment means I need to re-work that script in any case. Many thanks for coming back to me and apologies there has been such a delay in my response. – Steve Tomalin Oct 08 '21 at 07:58