Steve
Steve

Reputation: 1

AppActivate Function for windows with the same title and under one Process

I a using a very simple VBScript to switch between numerous screens and refreshing then before pausing and moving on, however two of the screens I want to switch between are named the same, any help appreciated.

Option Explicit
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
Do
    WScript.Sleep 3000
    WshShell.AppActivate("Inbox - Microsoft Outlook")
    WScript.Sleep 3000
    WshShell.AppActivate("CSE Task Monitor")
    WSHShell.SendKeys "{F5}" 
    WScript.Sleep 3000
    WshShell.AppActivate("website - Windows Internet Explorer")
    WSHShell.SendKeys "{F5}" 


Loop

Thanks all. Steve

Upvotes: 0

Views: 10045

Answers (1)

Erez H
Erez H

Reputation: 88

You should use ProcessId and not Window Title. I get the process ID from the exe file name.

Set WShell = CreateObject("WScript.Shell")
Set WMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
wql = "SELECT ProcessId FROM Win32_Process WHERE Name = ProcessName.exe'"
i=0
For Each process In WMI.ExecQuery(wql)
   ProcessIdArray(i) = process.ProcessId
i = i + 1
Next

Now you have array of processesIDs for the same ProcessName.exe and you can use

WshShell.AppActivate(ProcessIdArray(0))
WScript.Sleep 3000
WshShell.AppActivate(ProcessIdArray(1))

Upvotes: 2

Related Questions