Link to home
Start Free TrialLog in
Avatar of Gssc1414
Gssc1414Flag for United States of America

asked on

Need help with VB Script: Trying to see if program is open, if Is - Switch to it. If not, open it

I am trying to create a .vbs script that will do the following:

1. Look to see if a program is running.
1a. If the program is running, bring the program to the front of the screen. Kind of like ALT-TAB ing, but automatically.
1b. If the program is not running, start it.

I can look for and find the running program, and I can also start the program if it's not running. I cannot seem to figure out how to switch to the program if it's already running. I have been searching around, but cannot seem to find a solution - which is why im here...asking the experts.
Avatar of Shift-3
Shift-3
Flag of United States of America image

Here is one way to do it, using Microsoft Word's Tasks collection as a sort of makeshift Task Manager.  

Paste the script below into a text file with a .vbs extension.  Customize the value of the strTitle variable on line 1 with the window title as it appears on the taskbar.  Customize the value of the strProgram variable on line 2 with the location of the program to run if the window title isn't found.  Running the script will activate the window with that title or, if one isn't found, start the specified program.

Unfortunately this does require that Microsoft Word be installed on the machine.  I'm unaware of another way to change focus using native VBScript.


strTitle = "Program Name"
strProgram = "c:\program.exe"
 
Set objWord = CreateObject("Word.Application")
Set colTasks = objWord.Tasks
 
If colTasks.Exists(strTitle) Then
    colTasks(strTitle).Activate
    colTasks(strTitle).WindowState = wdWindowStateMaximize
Else
    Set objShell = WScript.CreateObject("WScript.Shell")
    objShell.Run strProgram
End If
 
objWord.Quit

Open in new window

Avatar of Gssc1414

ASKER

Shift-3,

Hmm... that's weird, using word? Ill give it a try, but I don't believe MS word is installed on the PC that this needs to work on.
Avatar of Bill Bach
What about a completely different solution?  A keyboard macro processing program like Keytext2000 (www.mjmsoft.com) can easily be scheduled to select an open window, create a new window, and even bring it to the front.  Of course, it does a lot more than this, and you have to pay for it, but it does include its own scheduler, which would make other aspects of this project a bit easier, and I suspect that you'd find it useful in other areas, too.
Just so that all you guys know, I will not abandon this question.... I am still trying to get a viable solution. I haven't been able to test your solution BillBach - I am hoping to this week.
ASKER CERTIFIED SOLUTION
Avatar of Bill Bach
Bill Bach
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial