How Can I Get a List of All the Senders’ Email Addresses in an Outlook Folder?

How Can I Get a List of All the Senders’ Email Addresses in an Outlook Folder?

  • Comments 3
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I get a list of all the senders’ email addresses in an Outlook folder?

-- CW

SpacerHey, Scripting Guy! AnswerScript Center

Hey, CW. You know, you must get email from a whole different set of people than we Scripting Guys do. For example, in looking through the Scripting Guys Inboxes it appears as though all our mail comes from the following sources:

Service representatives pointing out that our bank/eBay/PayPal account is about to expire, and noting that we need to log on to a specified Web site in order to prevent any interruption in service.

Evangelist Frank Radebe, the first son of the late Dr. John Radebe; Mr. Greg Stevenson, staff of Kleinwort Benson; Rev. Jones De; and Ms. Angela Dikko Emile, the daughter of Professor Emile Boga Doudou, all of whom need our help transferring money from an African bank to a U.S. bank. (But not as a favor to them, mind you; they’re more than willing to pay us for our trouble.)

Pharmaceutical salespeople who might be sampling their wares more than doctors recommend: “Tts h'erbal solution what hasnt no side effect, but has 100% guaranted resultss.”

Beautiful, sexy girls who got our names from a mutual friend and are just dying to meet the male Scripting Guys.

Note. OK, those last ones could be legitimate.

Well, they could be.

In other words, we Scripting Guys don’t have much need to get a list of all the people who send us email. (OK, that h’erbal solution might come in handy, but other than that …) Like we said, though, you might hang around with a different crowd than we do. If that’s the case, then a script like this one might be just the thing you need:

Const olFolderInbox = 6

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

Set colItems = objFolder.Items

For Each objItem in colItems
    Wscript.Echo objItem.SenderEmailAddress
Next

Yes, it is a nice little script, isn’t it? (We’re guessing that’s why Angela Dikko Emile came to us for help; she probably figured that if we could write a script like that then transferring money from an African bank to a U.S. bank would be a snap.) As you can see, we start out simply enough, defining a constant named olFolderInbox and setting the value to 6; in a minute or two we’ll use that constant to tell the script which Outlook folder we want to work with.

After defining the constant we create an instance of the Outlook.Application object, then use the GetNamespace method to bind to the MAPI namespace. (That’s actually the only namespace we can bind to, but we still have to bind to it. Kind of like having to fill out an income tax return even though they’ve already taken all your money.) After that we use the GetDefaultFolder method to bind us to the Inbox folder:

Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)

See: we told you we’d use the constant olFolderInbox to help us connect to the Inbox, didn’t we?

After connecting to the Inbox folder we can then use this line of code to retrieve a collection of all the items (i.e., all the email messages) found in that folder:

Set colItems = objFolder.Items

At this point all we have to do is set up a For Each loop and loop through the collection of email messages; for each message in that collection we simply echo back the value of the SenderEmailAddress property. Keep in mind, however, that when you try to access the SenderEmailAddress the following Outlook security dialog box is going to pop up onscreen:

Outlook


You’ll need to select the Allow access for check box, pick a time period, and then click Yes before the script will be allowed to retrieve anyone’s email address. (Yes, it’s a bit of a hassle. However, it does help prevent you from doing things like inadvertently notifying all the people who have sent you email that their PayPal account is about to expire.)

And that’s it: in a matter of seconds the sender email addresses for all the items in your Inbox will be displayed onscreen.

Like we said, this script retrieves information from your Inbox folders. What if you’d like to pull the list of email addresses from a subfolder of the Inbox? To tell you the truth, we weren’t sure if we could do that at first. However, after just one application of the miracle h’erbal solution (“100% guaranted resultss!”) we came up with this:

Const olFolderInbox = 6

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

Set objSubfolder = objFolder.Folders("Test")

Set colItems = objSubFolder.Items

For Each objItem in colItems
    Wscript.Echo objItem.SenderEmailAddress
Next

In this script we bind to – and retrieve email addresses from – a subfolder of the Inbox, a subfolder named Test. How do we do that? Well, after binding to the Inbox we use this line of code to connect us to the Test subfolder:

Set objSubfolder = objFolder.Folders("Test")

All we have to do then is make sure we retrieve the collection of emails from this subfolder:

Set colItems = objSubFolder.Items

Good question: that works if the folder is a subfolder on the Inbox. But what if the folder we’re interested in is on the same level as the Inbox? How the heck do we access a folder like that? Well, we’ll tell you, but you’ll have to wait until tomorrow’s column.

Unless, of course, it turns out that we really did win $5 million in the Euro lottery. Tune in tomorrow to find out.

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

    Cool script !

    For those people who don’t need to do this programmatically, here is simple way to extract the email address from any outlook folder (no purchase or download of anything) - check this out: thetechieguy.wordpress.com/.../extracting-email-address-from-outlook-2007-folder

    TheTechieGuy.wordpress.com

  • How can I know what other properties, objItem has in this script?

    Is there a way to find out?

    How do we reach it through the Outlook Object Model??

  • thank you