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

Posted on 2009-12-17
Medium Priority
Last Modified: 2012-05-08
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.
Question by:Gssc1414
  • 2
  • 2
LVL 38

Expert Comment

ID: 26074519
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


Author Comment

ID: 26074698

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.
LVL 29

Expert Comment

by:Bill Bach
ID: 26075285
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.

Author Comment

ID: 26109764
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.
LVL 29

Accepted Solution

Bill Bach earned 2000 total points
ID: 26112967
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!

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Why WooCommerce is one of the majorly favored choices when it comes to having an eCommerce store. This article will acquaint you with some reasons that I believe make it one of the best eCommerce platforms available.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question