Re: I Can't call WScript object methods vhen using my script through H by mayayana
mayayana
Tue Oct 10 08:20:44 CDT 2006
> Why can't I call e.g. the WScript.sleep method when calling my VBs
function
> through a HTA file?
As Trevor Sullivan said, you can't use anything
from WScript directly in HTML or HTA, just as you
can't use an IE document object model method
in a VBS file (unless you create an IE instance).
But you can still do a sleep fairly easily. You just
have to write a function in your HTA that calls a
VBS file.
This sample demonstrates it. Create two files,
sleeper.vbs and test.hta. In test.hta (or test.html)
enter this text:
(Watch out for word wrap.)
------------------ test.hta text ---------------------
<HTML>
<HEAD>
<SCRIPT LANGUAGE="VBScript">
Sub Sleep1(MSecs)
Dim Ret, SH
On Error Resume Next
Set SH = CreateObject("WScript.Shell")
Ret = SH.Run("sleeper.vbs " & MSecs, , True)
Set SH = Nothing
MsgBox CStr(MSecs \ 1000) & " seconds"
End Sub
</SCRIPT>
<TITLE></TITLE></HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<DIV align="left">
<input type="button" value="run sleep" onclick="Sleep1(8000)"></INPUT>
</DIV>
</BODY></HTML>
--------------------------- end test.hta text ------------------
Enter this text into sleeper.vbs and make sure it's in the
same location as test.hta:
------------------- sleeper.vbs text --------------------------
Dim Arg
on error resume next
Arg = WScript.Arguments(0)
wscript.sleep Arg
--------------- end sleeper.vbs text -----------------------------
When you click the button in test.hta it will pause
for 8 seconds and then show a msgbox. It works
because you can create WScript.Shell in webpage
scripting....and WScript.Shell can run a VBS file...and
a VBS file can use WScript.Sleep.
It may be confusing because "WScript.Shell" seems
to be part of WScript, but it's not. It's just poor planning
on the part of Microsoft when they were thinking up
ProgIDs. "WScript.Shell" is the object's ProgID. With
that you can create the object. WScript is just an
executable that hosts scripts. It doesn't have a ProgID,
so you can't create an instance of it. The WScript.xxx
functions are just part of the WScript "environment".