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>
Set objShell = WScript.CreateObject("WScript.Shell")
Set objIE = WScript.CreateObject("InternetExplorer.Application", "IE_")
objie.navigate "url"
objIE.Visible = 1
objShell.AppActivate objIE
'Above opens an ie object and navigates
'below runs through your proccesses and brings Internet Explorer to the top.
Set Processes = GetObject("winmgmts:").InstancesOf("Win32_Process")
intProcessId = ""
For Each Process In Processes
If StrComp(Process.Name, "iexplore.exe", vbTextCompare) = 0 Then
intProcessId = Process.ProcessId
Exit For
End If
Next
If Len(intProcessId) > 0 Then
With CreateObject("WScript.Shell")
.AppActivate intProcessId
End With
End If
I looked for acouple hours on the net today and scraped together this code. It actually works :D.