How Can I Filter Outlook Messages By Email Address?

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?


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] = ‘'”)

For Each objMessage In colFilteredItems
Wscript.Echo objMessage.Subject

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 That’s what this line of code is for:

Set colFilteredItems = colItems.Restrict(“[SenderEmailAddress] = ‘'”)

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., the criteria to be applied to the filter. In this case, we want the get back only emails where the SenderEmailAddress is equal to

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 or by

Set colFilteredItems = colItems.Restrict _
(“[SenderEmailAddress] = ‘’ OR [SenderEmailAddress] = ‘'”)

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.

Comments (3)

  1. Jazzyj1 says:

    Just read your article on filtering email, but what I more interested in is filtering by domain or even IP addresses to reduce or even stop spam.  Understanding that spammer uses many different email addresses, they can usually get around filters by simply spamming with a different prefix; example changing to would bypass any filter I may have set to stop Joeblow, thus making it necessary to create another filter for Joeblo.  I would much rather block his entire domain, thus any pefix used by him would get trapped by my filter. What suggestions if would you recommend to achieve this?

  2. Beginer says:

    Hey, i want to highlight the filtered ones in yellow and more over executing the above ends like object required for Wscript. Please help