Hey, Scripting Guy! How Can I Start Office Outlook If It Isn’t Already Running?

Hey, Scripting Guy! How Can I Start Office Outlook If It Isn’t Already Running?

  • Comments 1
  • Likes

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I determine if Office Outlook is running and, if it isn’t, start it?

-- NA

SpacerHey, Scripting Guy! AnswerScript Center

Hey, NA. You know, this past weekend the Scripting Guy who writes this column found himself wandering through the local shopping mall, a turn of events which left him very uneasy, almost fearful. Why? Well, the last time he’d been in the mall (during the height of the Christmas shopping rush) he’d been meandering along when he was approached by a young woman who spoke with a very heavy French accent. Due to both the accent and the noise inside the mall, the Scripting Guy who writes this column had difficulty understanding her at first; he assumed, however, that the woman was asking for directions or something. Because of that he tried focusing very hard on what the woman was saying. She must have mistaken this concentration for intense interest, because the next thing he knew she had poured salt all over his hands and was pantomiming that the Scripting Guy should now thoroughly “wash” his hands with the salt.

“Do you know which place on Earth has the lowest elevation?” she asked, a question which caught the Scripting Guy off guard. What, she needs directions to the Dead Sea?

“The Dead Sea?” replied the Scripting Guy.

“Oh,” she said, a little disappointed. “No one ever gets that right. Yes, the Dead Sea, and this is salt taken directly from the Dead Sea. If you rub it all over your hands and then wash it off you’ll be amazed how smooth your skin will be. Your hands will be as soft as a baby’s bottom.”

To her credit, she was right: once he got all the salt off, the Scripting Guy’s hands were as soft as a baby’s bottom. That was great, too, except for one problem: the Scripting Guy who writes this column doesn’t want his hands to be as soft as a baby’s bottom. Although he doesn’t consider himself a wimp, he apparently comes off as a wimp; for example, any time he plays basketball or football the first thing people try to do is run right over the top of him. Obviously, people aren’t all that impressed with his manliness to begin with; the last thing he needs is baby-soft hands. But, at least for a few hours, he had them.

We don’t know if you’re interested in having soft hands, NA; if you are, Dead Sea salt seems to do the trick. But if all you care about is a script that can determine whether or not Outlook is running and, if not, start it, well, then this should do the trick:

Const olFolderInbox = 6

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_Process Where Name = 'outlook.exe'")

If colItems.Count = 0 Then
    Set objOutlook = CreateObject("Outlook.Application")
    Set objNamespace = objOutlook.GetNamespace("MAPI")
    objNamespace.Logon "Default Outlook Profile",, False, True    
    Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)
    objFolder.Display
End If

We hope you can read that, NA; as it turns out, it’s hard to type with really soft hands. (Maybe that’s why babies don’t do much typing.) As you can see, we start out by defining a constant named olFolderInbox and setting the value to 6; we’ll use this constant when (or, more precisely, if) we open Outlook. Following that we connect to the WMI service on the local computer, then use this line of code to return a collection of all the processes on the computer whose Name property has the value outlook.exe:

Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_Process Where Name = 'outlook.exe'")

Note. Good question: could you run this script against a remote computer? Well, no, not really. You can easily determine whether or not Outlook is running on the remote machine, but you’ll run into problems when you try to start Outlook on that remote computer. Why? Because, for security reasons, processes started remotely always run in an invisible window; you’ll be able to start Outlook but you won’t be able see Outlook onscreen. Are there ways to work around this issue? Well, sort of; take a peek at this Hey, Scripting Guy! column for one suggestion.

When we call the ExecQuery method we get back a collection of all the processes running on the computer, or at least those processes that have the name Outlook.exe. To determine whether or not Outlook is running all we need to do is examine the value of the Count property, which tells us the number of items in the collection. If the Count is equal to anything other than 0 that means Outlook is already running and our job is finished.

But what if the Count is 0? In that case, Outlook isn’t, running; consequently, we need to use this block of code to start the application:

Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
objNamespace.Logon "Default Outlook Profile",, False, True    
Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)
objFolder.Display

This bit of code begins by creating an instance of the Outlook.Application object, then binds to the MAPI namespace, something you have to do even though this is the only namespace you can bind to. From there we call the Logon method, passing the following four parameters in order to ensure that we log on to the correct Outlook profile:

Parameter

Description

Profile

Name of the Outlook profile we want to connect to. This sample script uses “Default Outlook Profile”.

Password

Optional password to be used when connecting. This parameter exists primarily for backwards compatibility purposes and will almost always be left blank.

ShowDialog

Specifies whether the profile selection dialog box should be shown. We’ve already indicated that we want to use Default Outlook Profile, so there’s no reason to show this dialog box. Thus we set the value to False.

NewSession

Specifies whether a new Outlook session should be created. Because Outlook isn’t running we obviously need to create a new session. Thus we set this parameter to True.

The rest is easy: we use the GetDefaultFolder method to connect to the Inbox (using – at last! – the constant olFolderInbox) and then we call the Display method to make the Inbox, and the rest of Outlook, visible on screen. That’s all we have to do.

Incidentally, even though she was trying to sell him “beauty products” the Scripting Guy who writes this column actually felt sorry for the woman with the French accent; she couldn’t have picked a person less likely to buy – let alone use – Dead Sea salt. (At least not on his hands; maybe on his French fries.) “OK, so perhaps you aren’t concerned with smooth hands,” she said. “But let me ask you another question: what facial care products do you currently use?”

“Water.”

“No, no, I mean besides water.”

“Uh, soap?”

“Excuse me; I think someone over there has a question. Thank you for your time.”

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hello.

    I've running Outlook 2007 on Win XP machine and when i execute this script allways prompt password.

    What is wrong ?

    Dim WshShell

    Set WshShell=WScript.CreateObject("WScript.Shell")

    Const olFolderInbox = 6

    strComputer = "."

    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

    Set colItems = objWMIService.ExecQuery _

       ("Select * From Win32_Process Where Name = 'outlook.exe'")

    If colItems.Count = 0 Then

       MsgBox "Start Outlook"

       Set objOutlook = CreateObject("Outlook.Application")

       Set objNamespace = objOutlook.GetNamespace("MAPI")

       objNamespace.Logon "OCS BA", "myPassword", False, True

       Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)

       objFolder.Display

    Else

       MsgBox "O Outlook is already running"

    End If

    Thank you for help.

    Tiago Guimarães