Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wscript doesn't work in HTML file embedded with VBScript

Status
Not open for further replies.

sndk

Programmer
Feb 15, 2012
1
In the below script objShell works well but Wscript.run fails to identify the object.
<HTML>
<HEAD>
<TITLE>Test Button Events</TITLE>
</HEAD>
<BODY>
<FORM NAME="Form1">
<INPUT TYPE="Button" NAME="Button1" VALUE="Click">
<SCRIPT FOR="Button1" EVENT="onClick" LANGUAGE="VBScript">
set objShell = CreateObject("WScript.Shell")
Set fso=CreateObject("Scripting.FileSystemObject")

Wscript.run "Some xyz Function"
Wscript.Sleep 500
objShell.SendKeys "{Enter}"

</SCRIPT>
</FORM>
</BODY>
</HTML>
 
Try converting (renaming) to .hta unless you have a specific need for it to run in a browser.
 
You can not use the WScript.Sleep in an HTA Application.
But to circumvent this problem, you can use this trick :
Code:
Sub Sleep(MSecs) 
 Set fso = CreateObject("Scripting.FileSystemObject")
  If Fso.FileExists("sleeper.vbs")=False Then
  Set objOutputFile = fso.CreateTextFile("sleeper.vbs", True)
  objOutputFile.Write "wscript.sleep WScript.Arguments(0)"
  objOutputFile.Close
  End If
 CreateObject("WScript.Shell").Run "sleeper.vbs " & MSecs,1 , True
 End Sub
and your code become like this :
Code:
<HTML>
<HEAD>
<TITLE>Test Button Events</TITLE>
</HEAD>
<script language=vbscript>
   set objShell = CreateObject("WScript.Shell")
        Set fso=CreateObject("Scripting.FileSystemObject")        
        Sleep 5000
        objShell.SendKeys "{Enter}"
        
 Sub Sleep(MSecs) 
 Set fso = CreateObject("Scripting.FileSystemObject")
  If Fso.FileExists("sleeper.vbs")=False Then
  Set objOutputFile = fso.CreateTextFile("sleeper.vbs", True)
  objOutputFile.Write "wscript.sleep WScript.Arguments(0)"
  objOutputFile.Close
  End If
 CreateObject("WScript.Shell").Run "sleeper.vbs " & MSecs,1 , True
 End Sub  
 </SCRIPT>
</HTML>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top