How Can I Filter Outlook Messages By Email Address?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! I read your article on filtering email messages in Microsoft Outlook, and it’s 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?

— SJS

SpacerHey, Scripting Guy! AnswerScript Center

Hey, SJS. Say, you weren’t by any chance at the Columbia Athletic Club this past weekend, were you? We didn’t 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, “Listen, buddy, there’s no way that both of us are leaving this gym alive.” At first the Scripting Guy who writes this column didn’t 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:

“So Dad: are you gonna fight that guy over there?”

“Well, it kind of looks that way, doesn’t it?”

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.

Yes, very strange. Not that the Scripting Guy who writes this column isn’t used to people glaring at him with looks of utter hatred and disgust; he gets that at work pretty much every day. But it was a bit unusual for someone to glare at him at the gym. At the gym people are much more likely to giggle when he walks by.

And then burst out laughing when he tries doing the bench press.

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’t come up with any reason. And then he thought, “Hmmm, maybe that guy wanted to know how to do something like, say, filter Outlook email messages by email address. Maybe he wrote to Hey, Scripting Guy! and we haven’t answered him yet. Maybe we should answer his question and see if that makes him happy.”

If you were 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 weren’t the angry guy at the gym, well, do us a favor, OK: if you happen to run into the crazy guy, please don’t tell him that we answered your question instead of his?

Here’s how you (or the angry guy, for that matter) can filter Outlook email messages by email address:

Const olFolderInbox = 6

Set objOutlook = CreateObject(“Outlook.Application”) Set objNamespace = objOutlook.GetNamespace(“MAPI”) Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)

Set colItems = objFolder.Items Set colFilteredItems = colItems.Restrict(“[SenderEmailAddress] = ‘kenmyer@fabrikam.com'”)

For Each objMessage In colFilteredItems Wscript.Echo objMessage.Subject Next

Whoa, don’t 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 Outlook.Application object and bind to the MAPI namespace. We then use this line of code and the GetDefaultFolder method to make a connection to the Inbox folder:

Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)

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:

Set colItems = objFolder.Items

And you’re right: we don’t want all the items in the Inbox, do we? Instead, we want only those emails that were sent by kenmyer@fabrikam.com. That’s what this line of code is for:

Set colFilteredItems = colItems.Restrict(“[SenderEmailAddress] = ‘kenmyer@fabrikam.com'”)

All we’re doing here just grabbing a subset of the items found in the Inbox. To do that we call the Restrict method, passing the method two parameters:

[SenderEmailAddress], the property we want to filter on. Note that the property name must be enclosed in square brackets.

kenmyer@fabrikam.com, the criteria to be applied to the filter. In this case, we want the get back only emails where the SenderEmailAddress is equal to kenmyer@fabrikam.com.

Ah, that’s a good question: can we include multiple criteria in a filter? You bet we can. For example, here’s a filter that returns emails sent by kenmyer@fabrikam.com or by packerman@contoso.com:

Set colFilteredItems = colItems.Restrict _
    (“[SenderEmailAddress] = ‘kenmyer@fabrikam.com’ OR [SenderEmailAddress] = ‘packerman@contoso.com'”)

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’ll have to use that dialog box to explicitly grant the script access to the data. For more information, see our original article on filtering email messages.

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 Subject property; however, you can echo back the value of any (or all) of the properties of the MailItem object.

And there you have it, SJS: that should be enough to put a smile on anyone’s face.

We hope.

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’t know the meaning of the word fear.

It’s just a coincidence that he sent the Scripting Son out to start the car.

0 comments

Discussion is closed.

Feedback usabilla icon