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
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
    LVL 38

    Expert Comment

    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

    LVL 3

    Author Comment


    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 28

    Expert Comment

    by:Bill Bach
    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.
    LVL 3

    Author Comment

    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 28

    Accepted Solution

    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

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now