1

I've been searching for awhile, but I cannot seem to find the answer. I am making a gui based program selector and I am quite new to VBS and HTA. I've made a auto-typer and I cannot seem to figure out why it does not work in HTA. It works fine on its own.

<head>
<title>Gui Bases Program Selector.</title>
<HTA:APPLICATION 
     APPLICATIONNAME="HTA Test"
     SCROLL="yes"
     SINGLEINSTANCE="yes"
     WINDOWSTATE="maximize"
>
</head>

<script language="VBScript">
Sub TestSub
    Set shell = CreateObject("wscript.shell") 
    strtext  = InputBox("What Do you want your message do be?")
    strtimes = InputBox ("How many times would you like you type it?")
    If Not IsNumeric(strtimes) Then
        lol = MsgBox("Error = Please Enter A Number.") 
        WScript.Quit
    End If
    MsgBox "After you click ok the message will start in 5 seconds "
    WScript.Sleep(5000)
    Tor i=1 To strtimes
        shell.SendKeys(strtext & "")
        shell.SendKeys "{Enter}"
        WScript.Sleep(75)
    Next
End Sub
</script>

<body>
<input type="button" value="AutoTyper" name="run_button"  onClick="TestSub"><p> 
</body>
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • 1
    You don't have a variable named `wscript` in your code, so clearly you can't call it's `.quit` method. Learn to actually **read** the code you're writing or copy/pasting. If you actually read the code, the solution to the problem should jump right out at you. – Ken White Nov 07 '16 at 23:12
  • 1
    You are programming in Internet Explorer. For your programs there is NO WSCRIPT available, as the WSCript object is about controlling the script being run by WSCript (which yours isn't, it's being run by IE). Note other WScript objects (`wscript.shell`) are general purpose function libraries and are available to all COM programs anytime. More background here https://blogs.msdn.microsoft.com/ericlippert/2003/10/08/why-cant-i-create-the-wscript-object/ –  Nov 07 '16 at 23:26
  • IE's quit command is `window.close` –  Nov 07 '16 at 23:28
  • 1
    See http://stackoverflow.com/a/22053366/603855. The WScript object is pre-defined in scripts running from c|wscript.exe (@KenWhite) and is different/distinct from the COM object with the .Shell property (@Noodles). – Ekkehard.Horner Nov 08 '16 at 02:57
  • 1
    @Ekkehard.Horner: This is an IE HTA, not a script running from c|wscript. – Ken White Nov 08 '16 at 03:08
  • 1
    A fact that is *not* mentioned by your (@KenWhite) first comment and that does *not* 'jump right out' at the reader. – Ekkehard.Horner Nov 08 '16 at 03:17
  • 1
    @Ekkehard.Horner: This post asks about HTA. Why should I have to mention it in my comment? I don't have to say my comment is about C when posting to a C thread. And presumably the poster that said *I'm writing an HTA app* can be assumed to know it's a HTA app. Perhaps you should read the question before jumping in to correct people instead. You corrected both Noodles and me without knowing the topic at hand. – Ken White Nov 08 '16 at 03:21
  • @Ekkehard.Horner And you corrected me by saying the same thing as me. *For your programs there is NO WSCRIPT available, as the WSCript object is about controlling the script being run by WSCript* –  Nov 08 '16 at 03:33

1 Answers1

3

The HTA engine doesn't provide a WScript object, so things like WScript.Quit or WScript.Sleep don't work in HTAs. To programmatically exit from an HTA use Self.Close or window.Close. For replacing the Sleep method see the answers to this question.

Community
  • 1
  • 1
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328