开发者

VBScript set focus on a window in IE

开发者 https://www.devze.com 2023-03-06 01:45 出处:网络
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 VBS

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 = WSc开发者_运维问答ript.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:

'CreateObject("WScript.Shell")
Set wshShell = CreateObject("WScript.Shell")
wshShell.AppActivate("calculator")


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消