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.
Who is Participating?
Bill BachPresidentCommented:
When you're setting it up, here's help to get you started.  Go to Item/INsert Field Wizard.  Click on "Activate a window..." and click Next.  Select the first option "Activate (bring to the front) an existing window" and enter the title of the window into the box provided.  If you want to match the entire title text, then check "Match whole of title" as well. When you test it, if you have problems, you may need to ALSO check the "Force activate" box.  (I haven't needed it myself.)

Since you want to run the app if it's not running, also check the box for "if not found, run program instead" and click Next.  From that dialog, you'll have the option of entering the program name, parameters, starting directory, etc.  When done, it'll paste the macro text {Run...} into the item selected, and it is ready to go.  

To enable a schedule, select Item/Set Schedule and pick the schedule that you want to use, and it'll be ready to go!
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).WindowState = wdWindowStateMaximize
    Set objShell = WScript.CreateObject("WScript.Shell")
    objShell.Run strProgram
End If

Open in new window

Gssc1414Author Commented:

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.
Bill BachPresidentCommented:
What about a completely different solution?  A keyboard macro processing program like Keytext2000 ( 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.
Gssc1414Author Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.