问题
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>
回答1:
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:
'CreateObject("WScript.Shell")
Set wshShell = CreateObject("WScript.Shell")
wshShell.AppActivate("calculator")
回答2:
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.
回答3:
The trick is using WScript.CreateObject()
rather than plain CreateObject()
to create IE object.
Set objShell = WScript.CreateObject("WScript.Shell")
Set objIE = WScript.CreateObject("InternetExplorer.Application", "IE_")
objIE.Visible = 1
objShell.AppActivate objIE
P.S. I got the solution from Dan Bernhardt at https://groups.google.com/forum/#!msg/microsoft.public.scripting.vbscript/SKWhisXB4wY/U8cwS3lflXAJ
来源:https://stackoverflow.com/questions/5985997/vbscript-set-focus-on-a-window-in-ie