{"id":65663,"date":"2007-01-25T00:27:00","date_gmt":"2007-01-25T00:27:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/01\/25\/hey-scripting-guy-how-can-i-start-office-outlook-if-it-isnt-already-running\/"},"modified":"2007-01-25T00:27:00","modified_gmt":"2007-01-25T00:27:00","slug":"hey-scripting-guy-how-can-i-start-office-outlook-if-it-isnt-already-running","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-start-office-outlook-if-it-isnt-already-running\/","title":{"rendered":"Hey, Scripting Guy! How Can I Start Office Outlook If It Isn\u2019t Already Running?"},"content":{"rendered":"<h2><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\" class=\"nearGraphic\" \/> <\/h2>\n<p>Hey, Scripting Guy! How can I determine if Office Outlook is running and, if it isn&rsquo;t, start it?<\/p>\n<p>&#8212; NA<\/p>\n<p><img decoding=\"async\" height=\"5\" width=\"5\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" alt=\"Spacer\" border=\"0\" \/><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" class=\"nearGraphic\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" height=\"288\" width=\"120\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" align=\"right\" alt=\"Script Center\" border=\"0\" title=\"Script Center\" class=\"farGraphic\" \/><\/a><\/p>\n<p>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&rsquo;d been in the mall (during the height of the Christmas shopping rush) he&rsquo;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 &ldquo;wash&rdquo; his hands with the salt.<\/p>\n<p>&ldquo;Do you know which place on Earth has the lowest elevation?&rdquo; she asked, a question which caught the Scripting Guy off guard. What, she needs directions to the <i>Dead Sea<\/i>?<\/p>\n<p>&ldquo;The Dead Sea?&rdquo; replied the Scripting Guy.<\/p>\n<p>&ldquo;Oh,&rdquo; she said, a little disappointed. &ldquo;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&rsquo;ll be amazed how smooth your skin will be. Your hands will be as soft as a baby&rsquo;s bottom.&rdquo;<\/p>\n<p>To her credit, she was right: once he got all the salt off, the Scripting Guy&rsquo;s hands <i>were<\/i> as soft as a baby&rsquo;s bottom. That was great, too, except for one problem: the Scripting Guy who writes this column doesn&rsquo;t <i>want<\/i> his hands to be as soft as a baby&rsquo;s bottom. Although he doesn&rsquo;t consider himself a wimp, he apparently comes <i>off<\/i> 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&rsquo;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.<\/p>\n<p>We don&rsquo;t know if <i>you&rsquo;re<\/i> 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 <i>this<\/i> should do the trick:<\/p>\n<pre class=\"codeSample\">Const olFolderInbox = 6\n\nstrComputer = \".\"\n\nSet objWMIService = GetObject(\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\cimv2\")\n\nSet colItems = objWMIService.ExecQuery _\n    (\"Select * From Win32_Process Where Name = 'outlook.exe'\")\n\nIf colItems.Count = 0 Then\n    Set objOutlook = CreateObject(\"Outlook.Application\")\n    Set objNamespace = objOutlook.GetNamespace(\"MAPI\")\n    objNamespace.Logon \"Default Outlook Profile\",, False, True    \n    Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)\n    objFolder.Display\nEnd If<\/pre>\n<p>We hope you can read that, NA; as it turns out, it&rsquo;s hard to type with really soft hands. (Maybe that&rsquo;s why babies don&rsquo;t do much typing.) As you can see, we start out by defining a constant named olFolderInbox and setting the value to 6; we&rsquo;ll use this constant when (or, more precisely, <i>if<\/i>) 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 <b>Name<\/b> property has the value <i>outlook.exe<\/i>:<\/p>\n<pre class=\"codeSample\">Set colItems = objWMIService.ExecQuery _\n    (\"Select * From Win32_Process Where Name = 'outlook.exe'\")<\/pre>\n<table cellpadding=\"0\" cellspacing=\"0\" class=\"dataTable\" id=\"ECE\">\n<thead><\/thead>\n<tbody>\n<tr valign=\"top\" class=\"record\">\n<td>\n<p class=\"lastInCell\"><b>Note<\/b>. Good question: <i>could<\/i> 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&rsquo;ll run into problems when you try to <i>start<\/i> Outlook on that remote computer. Why? Because, for security reasons, processes started remotely always run in an invisible window; you&rsquo;ll be able to s<i>tart<\/i> Outlook but you won&rsquo;t be able <i>see<\/i> Outlook onscreen. Are there ways to work around this issue? Well, sort of; take a peek at this <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/sept05\/hey0906.mspx\"><b>Hey, Scripting Guy! column<\/b><\/a> for one suggestion.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>When we call the <b>ExecQuery<\/b> 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 <b>Count<\/b> 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.<\/p>\n<p>But what if the Count <i>is<\/i> 0? In that case, Outlook <i>isn&rsquo;t<\/i>, running; consequently, we need to use this block of code to start the application:<\/p>\n<pre class=\"codeSample\">Set objOutlook = CreateObject(\"Outlook.Application\")\nSet objNamespace = objOutlook.GetNamespace(\"MAPI\")\nobjNamespace.Logon \"Default Outlook Profile\",, False, True    \nSet objFolder = objNamespace.GetDefaultFolder(olFolderInbox)\nobjFolder.Display<\/pre>\n<p>This bit of code begins by creating an instance of the <b>Outlook.Application<\/b> object, then binds to the <b>MAPI<\/b> namespace, something you have to do even though this is the <i>only<\/i> namespace you can bind to. From there we call the <b>Logon<\/b> method, passing the following four parameters in order to ensure that we log on to the correct Outlook profile:<\/p>\n<table cellpadding=\"0\" cellspacing=\"0\" class=\"dataTable\" id=\"E5F\">\n<thead><\/thead>\n<tbody>\n<tr valign=\"top\" class=\"record\">\n<td>\n<p class=\"lastInCell\"><b>Parameter<\/b><\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\"><b>Description<\/b><\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" class=\"evenRecord\">\n<td>\n<p class=\"lastInCell\">Profile<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Name of the Outlook profile we want to connect to. This sample script uses &ldquo;Default Outlook Profile&rdquo;.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" class=\"record\">\n<td>\n<p class=\"lastInCell\">Password<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Optional password to be used when connecting. This parameter exists primarily for backwards compatibility purposes and will almost always be left blank.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" class=\"evenRecord\">\n<td>\n<p class=\"lastInCell\">ShowDialog<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Specifies whether the profile selection dialog box should be shown. We&rsquo;ve already indicated that we want to use Default Outlook Profile, so there&rsquo;s no reason to show this dialog box. Thus we set the value to False.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" class=\"record\">\n<td>\n<p class=\"lastInCell\">NewSession<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Specifies whether a new Outlook session should be created. Because Outlook isn&rsquo;t running we obviously need to create a new session. Thus we set this parameter to True.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>The rest is easy: we use the <b>GetDefaultFolder<\/b> method to connect to the Inbox (using &ndash; at last! &ndash; the constant olFolderInbox) and then we call the <b>Display<\/b> method to make the Inbox, and the rest of Outlook, visible on screen. That&rsquo;s all we have to do.<\/p>\n<p>Incidentally, even though she was trying to sell him &ldquo;beauty products&rdquo; the Scripting Guy who writes this column actually felt sorry for the woman with the French accent; she couldn&rsquo;t have picked a person less likely to buy &ndash; let alone use &ndash; Dead Sea salt. (At least not on his hands; maybe on his French fries.) &ldquo;OK, so perhaps you aren&rsquo;t concerned with smooth hands,&rdquo; she said. &ldquo;But let me ask you another question: what facial care products do you currently use?&rdquo;<\/p>\n<p>&ldquo;Water.&rdquo;<\/p>\n<p>&ldquo;No, no, I mean besides water.&rdquo;<\/p>\n<p>&ldquo;Uh, soap?&rdquo;<\/p>\n<p>&ldquo;Excuse me; I think someone over there has a question. Thank you for your time.&rdquo;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I determine if Office Outlook is running and, if it isn&rsquo;t, start it? &#8212; NA 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, [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[212,49,3,5],"class_list":["post-65663","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-outlook","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I determine if Office Outlook is running and, if it isn&rsquo;t, start it? &#8212; NA 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, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65663","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=65663"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65663\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=65663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}