Jump to content

objShell.Run problem


Recommended Posts

OK I am trying out some HTA scripting for a program I am writing. I need the ability to launch a program from a hidden control. Everything works, but the application closes immediately after opening it. However, if I put this action on a input button, it works fine.

Here is a working code example:

<script LANGUAGE="VBScript">Sub RunRestart
Set objShell = CreateObject("WScript.Shell")
objShell.Run "fscommand\restart.exe"
On Error Resume Next
Set objShell = Nothing
End Sub</SCRIPT>
<body>
<input id=runbutton style="width:153" class="button" type="button" value="Restart" name="restart_button" onClick="RunRestart">
</body>

Here is a non-working example

<script LANGUAGE="VBScript">Sub RunAdmin
Set objShell = CreateObject("WScript.Shell")
objShell.Run "fscommand\admin.exe"
On Error Resume Next
Set objShell = Nothing
End Sub</script>
<body><button id=runbutton style="width:0" class="button" type="hidden" value="Admin Prompt" accessKey="k" name="admin_button" onClick="RunAdmin"></body>

The main differences is that the working example uses an input object, while the other uses a button object. Setting the width to zero effectively hides the button. AccessKey allows me to use a keyboard shortcut to launch the program. If I change the onClick value in the "restart_button" object, the program launches properly.

What do you supposed is causing this behaviour?

Link to comment
Share on other sites


I think there's a very basic reason for your problem, buttonObject.type can be set to either "submit", "button" or "reset" and you have it set to "hidden"!

'Test run, use Alt-k to run the script from the hta

<script language="vbscript">
Sub RunAdmin
CreateObject("WScript.Shell").Run "fscommand\admin.exe"
End Sub
</script>
<body>
<button id=runbutton style="width:0" class="button" type="button" value="Admin Prompt" accesskey="k" name="admin_button" onClick="RunAdmin">
</body>

Link to comment
Share on other sites

That shouldn't make any difference, you can use accesskey with input as well as button:

'Test run, use Alt-k to run the script from the hta

<script language="vbscript">
Sub RunAdmin
CreateObject("WScript.Shell").Run "fscommand\admin.exe"
End Sub
</script>
<body>
<input id=runbutton style="width:0" class="button" type="button" value="Admin Prompt" accesskey="k" name="admin_button" onClick="RunAdmin">
</body>

Link to comment
Share on other sites

This worked using the Button Object.

<!--
Test Run Alt And K key
-->
<TITLE>Test Hta Access key</TITLE>
<script language="VBScript">
Sub RunAdmin
' CreateObject("Wscript.Shell").Run("fscommand\admin.exe"),1,true
alert "Test Alt And K"
End Sub</script>
<BODY>
<BUTTON
ID='RunButton'
Style='width:0;'
class='button'
value='Admin Promp'
accesskey='k'
onClick='RunAdmin'>
</BUTTON>
</BODY>

Link to comment
Share on other sites

Yes I think I have the code down. It seems to work either with button or input objects. The symptom I am having now is that the program it launches does not stay open. I was testing in VirtualBox, and I think this is an issue. Nevertheless, this post has allowed me to clean up my code a bit at least.

Link to comment
Share on other sites

The symptom I am having now is that the program it launches does not stay open.

If your object is being run like the example below

CreateObject("WScript.Shell").Run "fscommand\admin.exe"

Then change it to this to keep the program open

CreateObject("Wscript.Shell").Run("fscommand\admin.exe"),1,true

Link to comment
Share on other sites

  • 3 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...