{"id":65113,"date":"2007-04-10T21:02:00","date_gmt":"2007-04-10T21:02:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/04\/10\/how-can-i-filter-outlook-messages-by-email-address\/"},"modified":"2007-04-10T21:02:00","modified_gmt":"2007-04-10T21:02:00","slug":"how-can-i-filter-outlook-messages-by-email-address","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-filter-outlook-messages-by-email-address\/","title":{"rendered":"How Can I Filter Outlook Messages By Email Address?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! I read your article on <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/officetips\/aug05\/tips0818.mspx\"><B>filtering email messages<\/B><\/A> in Microsoft Outlook, and it\u2019s <I>almost<\/I> what I need, but not quite. What I really need to know is this: how can I filter Outlook messages by the email address of the person who sent the message?<BR><BR>&#8212; SJS<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, SJS. Say, you weren\u2019t by any chance at the Columbia Athletic Club this past weekend, were you? We didn\u2019t think so, but we thought we should ask. On Saturday the Scripting Guy who writes this column and the Scripting Son went to the gym to play basketball. As they stepped inside some stranger began glaring at the Scripting Guy who writes this column, as if to say, \u201cListen, buddy, there\u2019s no way that both of us are leaving this gym alive.\u201d At first the Scripting Guy who writes this column didn\u2019t pay much attention; having never seen this guy before, he assumed the man must be glaring at someone else. But then the Scripting Son spoke up:<\/P>\n<P>\u201cSo Dad: are you gonna fight that guy over there?\u201d<\/P>\n<P>\u201cWell, it kind of looks that way, doesn\u2019t it?\u201d<\/P>\n<P>In fact, the man in question glared at the Scripting Guy who writes this column as he and the Scripting Son checked in, and continued glaring as the two walked through the exercise room and headed for the basketball court. <\/P>\n<P>Yes, <I>very<\/I> strange. Not that the Scripting Guy who writes this column isn\u2019t used to people glaring at him with looks of utter hatred and disgust; he gets that at work pretty much every day. But it <I>was<\/I> a bit unusual for someone to glare at him at the gym. At the gym people are much more likely to <I>giggle<\/I> when he walks by. <\/P>\n<P>And then burst out laughing when he tries doing the bench press.<\/P>\n<P>At any rate, the Scripting Guy who writes this column spent the weekend wondering why a total stranger would glare at him like that. At first he couldn\u2019t come up with any reason. And then he thought, \u201cHmmm, maybe that guy wanted to know how to do something like, say, filter Outlook email messages by email address. Maybe he wrote to <I>Hey, Scripting Guy!<\/I> and we haven\u2019t answered him yet. Maybe we should answer his question and see if <I>that<\/I> makes him happy.\u201d<\/P>\n<P>If you <I>were<\/I> the angry guy at the gym, SJS, we hope this helps, and we apologize for taking so long to get around to your question. And if you <I>weren\u2019t<\/I> the angry guy at the gym, well, do us a favor, OK: if you happen to run into the crazy guy, please don\u2019t tell him that we answered your question instead of his?<\/P>\n<P>Here\u2019s how you (or the angry guy, for that matter) can filter Outlook email messages by email address:<\/P><PRE class=\"codeSample\">Const olFolderInbox = 6<\/p>\n<p>Set objOutlook = CreateObject(&#8220;Outlook.Application&#8221;)\nSet objNamespace = objOutlook.GetNamespace(&#8220;MAPI&#8221;)\nSet objFolder = objNamespace.GetDefaultFolder(olFolderInbox)<\/p>\n<p>Set colItems = objFolder.Items\nSet colFilteredItems = colItems.Restrict(&#8220;[SenderEmailAddress] = &#8216;kenmyer@fabrikam.com'&#8221;)<\/p>\n<p>For Each objMessage In colFilteredItems\n    Wscript.Echo objMessage.Subject\nNext\n<\/PRE>\n<P>Whoa, don\u2019t look at us like that; we were just about to explain how the script works. (Really.) We start out by defining a constant named olFolderInbox and setting the value to 6; that tells the script which Outlook folder (in this case the Inbox folder) we want to work with. Next we create an instance of the <B>Outlook.Application<\/B> object and bind to the <I>MAPI<\/I> namespace. We then use this line of code and the <B>GetDefaultFolder<\/B> method to make a connection to the Inbox folder:<\/P><PRE class=\"codeSample\">Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)\n<\/PRE>\n<P>Now for the good stuff. The first thing we do is use this line of code to return a collection of all the items (that is, all the email messages) found in the Inbox folder:<\/P><PRE class=\"codeSample\">Set colItems = objFolder.Items\n<\/PRE>\n<P>And you\u2019re right: we <I>don\u2019t<\/I> want all the items in the Inbox, do we? Instead, we want only those emails that were sent by <I>kenmyer@fabrikam.com<\/I>. That\u2019s what this line of code is for:<\/P><PRE class=\"codeSample\">Set colFilteredItems = colItems.Restrict(&#8220;[SenderEmailAddress] = &#8216;kenmyer@fabrikam.com'&#8221;)\n<\/PRE>\n<P>All we\u2019re doing here just grabbing a subset of the items found in the Inbox. To do that we call the <B>Restrict<\/B> method, passing the method two parameters:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>[SenderEmailAddress]<\/B>, the property we want to filter on. Note that the property name must be enclosed in square brackets.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>kenmyer@fabrikam.com<\/B>, the criteria to be applied to the filter. In this case, we want the get back only emails where the SenderEmailAddress is equal to <I>kenmyer@fabrikam.com<\/I>.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>Ah, that\u2019s a good question: <I>can<\/I> we include multiple criteria in a filter? You bet we can. For example, here\u2019s a filter that returns emails sent by kenmyer@fabrikam.com <I>or<\/I> by packerman@contoso.com:<\/P><PRE class=\"codeSample\">Set colFilteredItems = colItems.Restrict _\n    (&#8220;[SenderEmailAddress] = &#8216;kenmyer@fabrikam.com&#8217; OR [SenderEmailAddress] = &#8216;packerman@contoso.com'&#8221;)\n<\/PRE>\n<P>One thing to keep in mind here is the fact that, because we are dealing with email addresses, Outlook security might kick in when we run this script. That means that a dialog box might pop up telling us that someone (or something) is trying to access email addresses; in turn, we\u2019ll have to use that dialog box to explicitly grant the script access to the data. For more information, see our <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/officetips\/aug05\/tips0818.mspx\"><B>original article<\/B><\/A> on filtering email messages.<\/P>\n<P>After applying the filter we then set up a For Each loop to walk through all the items in the filtered collection. In this sample script all we do is echo back the value of the <B>Subject<\/B> property; however, you can echo back the value of any (or all) of the properties of the <A href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/aa210946(office.11).aspx\" target=\"_blank\"><B>MailItem<\/B><\/A> object. <\/P>\n<P>And there you have it, SJS: that should be enough to put a smile on anyone\u2019s face. <\/P>\n<P>We hope.<\/P>\n<P>Speaking of which, the trip to the gym turned out to be somewhat anticlimactic: by the time the Scripting Guy and Scripting Son finished playing basketball the angry man had left the building. Was the Scripting Guy who writes this column shaken by this experience? What, are you kidding? The Scripting Guy who writes this column doesn\u2019t know the meaning of the word <I>fear<\/I>.<\/P>\n<P>It\u2019s just a coincidence that he sent the Scripting Son out to start the car.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I read your article on filtering email messages in Microsoft Outlook, and it\u2019s almost what I need, but not quite. What I really need to know is this: how can I filter Outlook messages by the email address of the person who sent the message?&#8212; SJS Hey, SJS. Say, you weren\u2019t by [&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-65113","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! I read your article on filtering email messages in Microsoft Outlook, and it\u2019s almost what I need, but not quite. What I really need to know is this: how can I filter Outlook messages by the email address of the person who sent the message?&#8212; SJS Hey, SJS. Say, you weren\u2019t by [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65113","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=65113"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65113\/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=65113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}