VBScript set focus on a window in IE

37,806

Solution 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")

Solution 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.

Share:
37,806
jtpereyda
Author by

jtpereyda

Maintainer of boofuzz. "If you're going to make a backward-compatibility-breaking change, no time is better than now; things will be worse in the future." Eric Lippert Sharp Regrets: Top 10 Worst C# Features

Updated on January 15, 2020

Comments

  • jtpereyda
    jtpereyda over 4 years

    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>
    
    • Tmdean
      Tmdean about 13 years
      AppActivate searches using the text in the window's title bar. What code were you using for IE?
    • jtpereyda
      jtpereyda about 13 years
      @Tmdean What I mean is that the code is in a <script type="text/vbscript"> tag and being run in the browser. I was actually calling AppActivate with "WINWORD" (process name), but the code is crashing on the first line where I try to make the WshShell object.
    • Tmdean
      Tmdean about 13 years
      WScript is only available when you run the script using Windows Scripting Host (using wscript.exe or cscript.exe). Also, there are security restrictions that prevent you getting access to a WScript.Shell object in a web page because that object is not marked as safe for scripting. It would be very dangerous if scripts running on web pages could use it. See stackoverflow.com/questions/1363095/…
    • jtpereyda
      jtpereyda about 13 years
      @Tmdean Thanks for the info. I'm programming for an intranet, and it looks like the default settings are fairly lax. This question says that I should enable all ActiveX stuff, and all of those settings are already set to Enable.
  • jtpereyda
    jtpereyda about 13 years
    I found that as well, but it gives me another error message, "Object doesn't support this property or method: 'WScript.CreateObject'".
  • jtpereyda
    jtpereyda about 13 years
    Do you know the exact security setting in question? My settings are pretty low (almost everything is enabled under Security Settings). See comment on question.
  • Tmdean
    Tmdean about 13 years
    I think you are still using WScript.CreateObject("WScript.Shell"). You should only be using CreateObject("WScript.Shell")
  • Kevin Brown-Silva
    Kevin Brown-Silva over 9 years
    While this answer is probably correct and useful, it is preferred if you include some explanation along with it to explain how it helps to solve the problem. This becomes especially useful in the future, if there is a change (possibly unrelated) that causes it to stop working and users need to understand how it once worked.
  • Admin
    Admin over 6 years
    I would've accepted this answer, personally. It's the most reliable answer I could find. +1 :)