{"id":68633,"date":"2005-10-31T13:28:00","date_gmt":"2005-10-31T13:28:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/10\/31\/how-can-i-start-an-application-from-an-hta\/"},"modified":"2005-10-31T13:28:00","modified_gmt":"2005-10-31T13:28:00","slug":"how-can-i-start-an-application-from-an-hta","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-start-an-application-from-an-hta\/","title":{"rendered":"How Can I Start an Application From an HTA?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! Is there an alternative to the Wscript.Shell command for HTAs? I need to run an application and specify the file to open.<BR><BR>&#8212; DL<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, DL. Yes, we <I>do<\/I> know of an alternative to the Wscript.Shell command that will work in HTAs, and we\u2019ll show you that in a minute. Before we do that, however, we should note that you actually <I>can<\/I> use the Wscript.Shell object within an HTA. This is a point that often creates confusion: because you can\u2019t use certain commands &#8211; such as Wscript.Echo and Wscript.Sleep &#8211; within an HTA people assume you can\u2019t use <I>any<\/I> WSH commands in an HTA. <\/P>\n<P>First things first: why can\u2019t you use Wscript.Echo and Wscript.Sleep in an HTA? Well, those methods are properties of the Wscript object, and you can\u2019t create an instance of the Wscript object. Instead, the Wscript object is automatically created &#8211; and only created &#8211; when you run Windows Script Host (that is, Wscript.exe or Cscript.exe). That\u2019s why this is a perfectly valid script:<\/P><PRE class=\"codeSample\">Wscript.Echo &#8220;Hey.&#8221;\n<\/PRE>\n<P>Notice that we didn\u2019t create the Wscript object; instead, it was created for us when we invoked Windows Script Host.<\/P>\n<P>But that\u2019s just for the Wscript object. There are other WSH objects that you <I>can<\/I> create, including the Shell object. For example, here\u2019s a simple little HTA that creates the Wscript.Shell object and then runs Notepad.exe (opening the file C:\\Scripts\\Test.txt along the way):<\/P><PRE class=\"codeSample\">&lt;html&gt; \n&lt;head&gt; \n&lt;script language=&#8221;VBScript&#8221;&gt; <\/p>\n<p>    Sub RunProgram \n        Set objShell = CreateObject(&#8220;Wscript.Shell&#8221;)\n        objShell.Run &#8220;notepad.exe c:\\scripts\\test.txt&#8221;\n    End Sub<\/p>\n<p>&lt;\/script&gt; \n&lt;\/head&gt; <\/p>\n<p>&lt;body&gt; \n&lt;button onclick=&#8221;RunProgram&#8221;&gt;Run Program&lt;\/button&gt; &lt;p&gt;\n&lt;\/body&gt; \n&lt;\/html&gt;\n<\/PRE>\n<P>As you can see, this is about as simple an HTA as you can get: it consists entirely of a button that, when clicked, runs a subroutine named RunProgram. And take a look at the code for RunProgram:<\/P><PRE class=\"codeSample\">Sub RunProgram \n    Set objShell = CreateObject(&#8220;Wscript.Shell&#8221;)\n    objShell.Run &#8220;notepad.exe c:\\scripts\\test.txt&#8221;\nEnd Sub\n<\/PRE>\n<P>There it is: we create an instance of the <B>Wscript.Shell<\/B> object and then call the <B>Run<\/B> method. And in doing so we pass Run a single parameter: the executable file name (<B>notepad.exe<\/B>) followed by the path to the file we want to open. That\u2019s all we have to do.<\/P>\n<P>Incidentally, as long as you run this in an HTA everything will work just fine. If you try to run it in an HTML file (that is, a file with a .htm file extension) you\u2019ll be presented with a message box warning you that an ActiveX control is trying to run on the page. At that point you\u2019ll have to click <B>Yes<\/B> to allow the subroutine to create the Shell object and then run. This is due to the fact that WSH objects are considered \u201cunsafe for scripting.\u201d <\/P>\n<TABLE class=\"dataTable\" id=\"E4D\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. Yes, it sounds a bit weird that scripting objects aren\u2019t considered safe for scripting. But that\u2019s because Internet Explorer uses a different script host and a different security model than WSH. Fortunately, HTAs use a different security model than Internet Explorer, which means you won\u2019t encounter this problem when creating the Shell object within an HTA.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Now, what about that alternative? Well, if for some reason you don\u2019t want to use the Wscript.Shell object then you can use the Windows Shell object instead. This HTA will also start Notepad and open the file C:\\Scripts\\Test.txt:<\/P><PRE class=\"codeSample\">&lt;html&gt; \n&lt;head&gt; \n&lt;script language=&#8221;VBScript&#8221;&gt; <\/p>\n<p>Sub RunProgram \n    Const NORMAL_WINDOW = 1\n    Set objShell = CreateObject(&#8220;Shell.Application&#8221;)\n    objShell.ShellExecute &#8220;notepad.exe&#8221;, &#8220;c:\\scripts\\test.txt&#8221;, , , NORMAL_WINDOW\nEnd Sub<\/p>\n<p>&lt;\/script&gt; \n&lt;\/head&gt; <\/p>\n<p>&lt;body&gt; \n&lt;button onclick=&#8221;RunProgram&#8221;&gt;Run Program&lt;\/button&gt; &lt;p&gt;\n&lt;\/body&gt; \n&lt;\/html&gt;\n<\/PRE>\n<P>To be honest, we don\u2019t see any real advantage to using the Windows Shell object as opposed to the Wscript.Shell object: both objects do pretty much the same thing. However, if you\u2019d like to play around a bit with the Windows Shell, check out the documentation on the <A href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/shellcc\/platform\/shell\/reference\/objects\/ishelldispatch2\/shellexecute.asp\" target=\"_blank\"><B>ShellExecute<\/B><\/A> method. The important thing is this: if you want to start an application from your HTA, either approach will work.<\/P><BR>\n<DIV>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"\"><A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/oct05\/hey1031.mspx#top\"><IMG height=\"9\" alt=\"Top of page\" src=\"http:\/\/www.microsoft.com\/technet\/mnplibrary\/templates\/MNP2.Common\/images\/arrow_px_up.gif\" width=\"7\" border=\"0\"><\/A><A class=\"topOfPage\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/oct05\/hey1031.mspx#top\">Top of page<\/A><\/TD><\/TR><\/TBODY><\/TABLE><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Is there an alternative to the Wscript.Shell command for HTAs? I need to run an application and specify the file to open.&#8212; DL Hey, DL. Yes, we do know of an alternative to the Wscript.Shell command that will work in HTAs, and we\u2019ll show you that in a minute. Before we do [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,5,30],"class_list":["post-68633","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-web-pages-and-htas"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! Is there an alternative to the Wscript.Shell command for HTAs? I need to run an application and specify the file to open.&#8212; DL Hey, DL. Yes, we do know of an alternative to the Wscript.Shell command that will work in HTAs, and we\u2019ll show you that in a minute. Before we do [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68633","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=68633"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68633\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=68633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}