I’m updating an old piece of code that uses VBScript to pull up a window in IE. For some reason, it likes to open up behind IE. Google gave me the following couple lines for setting window focus in VBScript:
set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate("calculator")
However, when I run this in IE, I get the error “Object required: ‘WScript’.”
Is there any way around this in IE, or another way to do this? I’m already opening and manipulating a Word document without any problem.
Edit: To clarify, I am running this in a <script type=”text/vbscript”> tag in the browser (IE), and the code is crashing on the first line, before I even call AppActivate.
Update: My security settings are pretty low; all ActiveX settings are on Enable (this is an intranet service). I tested the code from this question, and the calculator opened without issue. In fact, I got AppActivate to work with JavaScript, but it’s not working with VBScript.
Working JavaScript:
<script type="text/javascript">
function calcToFrontJ(){
wshShell = new ActiveXObject("WScript.Shell");
wshShell.AppActivate("Calculator");
}
</script>
Not Working VBScript:
<script type="text/vbscript">
Public Function calcToFrontV()
'Set WScript = CreateObject("WScript.Shell") 'breaks with or without this line
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate("Calculator")
End Function
</script>
I guess I can always refactor to JavaScript, but I’d really like to know what’s going on with this VBScript.
Final Answer:
<script type="text/vbscript">
Public Function calcToFrontV()
'must not use WScript when running within IE
Set WshShell = CreateObject("WScript.Shell")
WshShell.AppActivate("Calculator")
End Function
</script>
The WScript object does not exist in IE unless you create it yourself with:
Set WScript = CreateObject("WScript.Shell")But it won’t work if security settings are not at a quite low level.
Edit: Factoring in Tmdean’s comment, this is the working code: