1

Is it possible to use wshshell.appactivate to switch between multiple internet explorer windows?

I am trying to write a script that will bring different instances of internet explorer in kiosk mode to the front. The end goal is to make it look like it's going from one web page to the next. I tried using wshshell.sendkey but it doesn't look smooth with the open webpage dialog popping up.

Thisandthat
  • 21
  • 2
  • 4

2 Answers2

1

I played with this for a few days. We can get an hwnd from IE, but not a PID. So the only way I can see to match HWnd to PID is call a Win32API call. So how to do that in VBS.

All computers have 4 VB.NET compilers installed. So all we need do is write a com server that wraps GetWindowThreadProcessId.

In your script write out the following line to a text file. I repurposed a different script for this so method names are silly.

Imports System
Imports System.Runtime.InteropServices
Imports Microsoft.Win32
Imports System.Net.Mail



Namespace SendMail

    <Guid("85B4AD6D-2E89-4869-9BBC-69E42738FCFC"), _
    InterfaceType(ComInterfaceType.InterfaceIsIDispatch)> _
   Public Interface _SendMail
        <DispId(1)> Function Send(ByVal hWnd As Integer) As Integer
    End Interface

    <Guid("C91EDEB2-3756-4893-905B-0E4E2150C7FD"), _
     ClassInterface(ClassInterfaceType.None), _
     ProgId("Scripting.SendMail")> Public Class SendMail
        Implements _SendMail

        Public SendMail()
        Public Declare Auto Function GetWindowThreadProcessId Lib "user32" Alias "GetWindowThreadProcessId" (ByVal hwnd As Integer, ByRef lpdwProcessId As Integer) As Integer

        Public Function Send(HWnd as Integer) As Integer Implements _SendMail.Send
            Dim X as Integer
            Dim M as Integer
    M=1

            X=GetWindowThreadProcessID(HWnd,M)
            msgbox(X & " " & M & " " & HWnd & " " & Err.LastDllError)
            Send = M

        End Function

    End Class

End Namespace

Then to compile WSHShell.Run the following commands hidden fixing paths.

"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:library /out:"%userprofile%\desktop\sendmail\sendmail.dll" "%userprofile%\desktop\sendmail\sendmail.cls" /verbose


"C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm" /codebase "%userprofile%\desktop\sendmail\sendmail.dll" /tlb:"%userprofile%\desktop\sendmail\sendmail.tlb" /v

Then to use in script

Set x = CreateObject("Scripting.SendMail")
Msgbox x.Send(&h1a013e)

Now I've generated the GUID's for this purpose of creating com objects on the fly. As they are in now public code you (AND ANYONE ELSE COPYING THIS) must destroy the object in your script. Run the Regasm command with /u. Or generate new GUIDs.

trigger
  • 76
  • 1
0

You can enumerate all explorer/internet explorer windows.

From Help

Windows Method


Creates and returns a ShellWindows object. This object represents a collection of all of the open windows that belong to the Shell.

Syntax

oWindows = Shell.Windows()

Return Value

Object reference to the ShellWindows object.

Examples

The following example uses Windows to retrieve the ShellWindows object and display a count of the number of items that it contains. Proper usage is shown for Microsoft JScript, Microsoft Visual Basic Scripting Edition (VBScript), and Visual Basic. VBScript:

Show Example

<script language="VBScript">
    function fnShellWindowsVB()
        dim objShell
        dim objShellWindows

        set objShell = CreateObject("Shell.Application")
        set objShellWindows = objShell.Windows

        if (not objShellWindows is nothing) then
            alert(objShellWindows.Count)
        end if

        set objShellWindows = nothing
        set objShell = nothing
    end function
 </script>

But I don't know how this would help unless they have different titlebar text. Appactivate is the only windows' management command available to scripting.

Also appactivate returns a value saying if it made the window active or not. If you try to activate an active window it fails as the window is already active.

You can do process switching, but usually web pages are in the same process. AppActivate takes a PID

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * From Win32_Process")

For Each objItem in colItems
    msgbox objItem.ProcessID & " " & objItem.Caption
Next
tony bd
  • 82
  • 2
  • _"If you try to activate an active window it fails as the window is already active."_ I don't believe this is true. It should return `True` regardless of whether the window was already active prior to the call. – Bond Apr 30 '14 at 19:41
  • Internet explorer does have unique PID but there is no way of knowing what number they will be assigned when started. I don't know anyway of searching through processes and finding PID numbers. – Thisandthat Apr 30 '14 at 23:19
  • @Bond. It's true with either PuTTY or NetTerm. But perhaps they are unusual. – tony bd May 01 '14 at 05:02