Launching an application from an HTA _hta

Source: Internet
Author: User
Tags sleep
How do I start an application from an HTA?
Ask:
Hello, Scripting Guy! Are there any alternatives to the Wscript.Shell command for HTA? I need to run an application and specify the file to open.
--DL
For:
Hello, DL. Yes, we do know that such a command can be used in an HTA and can replace the Wscript.Shell command, which we'll be introducing to you in a moment. Before introducing it, however, we should note that you can actually use the Wscript.Shell object in your HTA. This is an often confusing place: because you can't use certain commands (such as WScript.Echo and Wscript.Sleep) in an HTA, people think you can't use any of the WSH commands in an HTA.
Before continuing, there is another question: Why can't you use WScript.Echo and Wscript.Sleep in an HTA? Yes, these methods are properties of the Wscript object, and you cannot create Wscript object instances. The Wscript object is created automatically and is only created when you run Windows Script Host (that is, Wscript.exe or Cscript.exe). Because of this, it is possible to say that the following script is absolutely valid:
Copy Code code as follows:

WScript.Echo "Hey."

Notice that we did not create the Wscript object, which was created automatically when we called Windows Script Host.
But this is limited to Wscript objects. There are other WSH objects that you can create, including Shell objects. For example, here is a simple little HTA that creates a Wscript.Shell object and then runs Notepad.exe (and opens the file C:\Scripts\Test.txt in this procedure):
Copy Code code as follows:

<script language= "VBScript" >
Sub Runprogram
Set Objshell = CreateObject ("Wscript.Shell")
Objshell.run "notepad.exe C:\Scripts\Test.txt"
End Sub
</script>
<body>
<button onclick= "Runprogram" >run program</button> <p>
</body>

As you can see, this is almost the simplest HTA you can get: it's made up of just one button, and a subroutine named Runprogram will run when clicked. Take a look at the Runprogram code:
Copy Code code as follows:

Sub Runprogram
Set Objshell = CreateObject ("Wscript.Shell")
Objshell.run "notepad.exe C:\Scripts\Test.txt"
End Sub

That's it: we create an instance of the Wscript.Shell object and call the Run method. This passes a single parameter for Run: The executable file name (notepad.exe) followed by the path to the file you want to open. That's all we need to do.
By the way, if you run the script in an HTA, there's no problem. If you try to run it in an HTML file (that is, a file with a. htm file name extension), a message box warns you that an ActiveX control is trying to run on the page. You must click Yes to allow the subroutine to create the Shell object and then run it. This is because the WSH object is considered "unsafe for scripting".
Note: Yes, that sounds a little weird, and it's thought that scripting objects are not safe for scripting. But this is because Internet Explorer uses a different script host and a different security model than WSH. Luckily, the HTA uses a different security model than Internet Explorer, which means that you do not experience this problem when you create a Shell object within an HTA.
So, what if the alternative command is used? Well, if for some reason you don't want to use the Wscript.Shell object, you can use the Windows Shell object instead. The following HTA can also start Notepad and open file C:\Scripts\Test.txt:
Copy Code code as follows:

<script language= "VBScript" >
Sub Runprogram
Const Normal_window = 1
Set Objshell = CreateObject ("Shell.Application")
Objshell.shellexecute "notepad.exe", "C:\Scripts\Test.txt",,, Normal_window
End Sub
</script>
<body>
<button onclick= "Runprogram" >run program</button> <p>
</body>

Frankly, we think that using a Windows Shell object does not have a real advantage over the use of the Wscript.Shell object: The functions of the two objects are essentially the same. However, if you want to fiddle with Windows Shell, consult the documentation for the ShellExecute method. This is important: if you want to start an application from an HTA, both methods are available.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.