1

I have the process ID of the process for my application that is already running. Now I want to activate that same window through this process id with VBScript.

Supposing mypid is the process ID

Set Shell = CreateObject("WScript.Shell")
Shell.AppActivate(mypid)

This is returning false on Windows 7.

If anyone has an answer, I would like to know it.

iBug
  • 35,554
  • 7
  • 89
  • 134
Rohit
  • 11
  • 1
  • 2
  • @eurotrash `retrn = shell.AppActivate(mypid)` is right approach. The `AppActivate` method returns a `Boolean` value that identifies whether the procedure call is successful. This method changes the focus to the named application or window, but it does not affect whether it is maximized or minimized. Focus moves from the activated application window when the user takes action to change the focus (or closes the window). – JosefZ Dec 29 '14 at 21:43
  • What do you mean by "open that same window"? If the process is running, the window should already be open. Do you want to bring the window to the front? – Ansgar Wiechers Dec 30 '14 at 00:57
  • I want to bring that window to front. It doesn't do that. – Rohit Dec 30 '14 at 09:03
  • shell.appactivate mypid is same as shell.appactivate(mypid) – Rohit Dec 30 '14 at 09:04
  • also if you don't save the return value, still command runs – Rohit Dec 30 '14 at 09:04
  • I am sure of syntax. I just need that concept by which I could bring that application to front so that I can press some keys in automation of that screen. – Rohit Dec 30 '14 at 09:05
  • I think I've lost my vbscript knowledge. I'll delete my comments since evidently I was incorrect. – 404 Dec 30 '14 at 09:06
  • that's ok ;) we are here to learn. If you could help me by your experience, that would be great – Rohit Dec 30 '14 at 09:09
  • I've used AppActivate in the past with some stuff and I seem to recall having trouble sometimes activating windows via the PID. Could you try to get the window title via the PID (by parsing a tasklist command which displays the window title) and then activating the window via the title? I think that was my workaround back then. – 404 Dec 30 '14 at 09:10
  • How can I activate window via the title ? – Rohit Dec 30 '14 at 09:12
  • Same way as with the PID: see this [MSDN link](http://msdn.microsoft.com/en-us/library/wzcddbek(v=vs.84).aspx). – 404 Dec 30 '14 at 09:16
  • To get the window title you could do something like `Set x = shell.Exec("tasklist /fi ""pid eq " & mypid & """ /fo list /v")` and then look for a line in stdout containing "Window Title" and grab the window title from that, then pass it to your AppActivate statement. – 404 Dec 30 '14 at 09:21
  • appactivate is still working the same. – Rohit Dec 30 '14 at 10:04
  • It makes the application icon in taskbar flashing but doesn't bring the application in focus – Rohit Dec 30 '14 at 10:04
  • Normally `AppActivate` should both set the focus to the window of the given process and bring it to the foreground. However, if the call to `SetForegroundWindow()` fails reproducibly there is nothing else you could do in VBScript. Try a different tool ([AutoIt](https://www.autoitscript.com/site/autoit/) might work from what I've heard). – Ansgar Wiechers Dec 30 '14 at 11:33

1 Answers1

1

Check this link : WshShell.AppActivate doesn't seem to work in simple vbs script

And this is the summary of good solution there:

Set WshShell = CreateObject("WScript.Shell")
    for i=0 to 300    'this loop will continue about 30 sec if this not enough increase this number
        Rtn=WshShell.AppActivate(myPID)  'have to be the windows title of application or its process ID
        If Rtn = True Then 
            WshShell.SendKeys "......."                                  ' send key you like
            wscript.sleep 100                ' stop execute next line until finish close app
             End If 
        wscript.sleep 100
    Next