Get list of “smtp” email addresses from Outlook messages

I had recently to export the email addresses contained in the BCC: field of an Outlook message. It turned out it’s not that easy. It achieved it with the following of Visual Basic, which will let you pick a folder and dump all recipients for all messages in this folder. I wanted to do it for a specific message and copy the result to the clipboard, but neither of this looks easy in Outlook. I spent already too much time on this so I give up, this version is good enough for me.

Sub ExtractRecipientsFromEmail()
Dim OlApp As Outlook.Application
Dim MailObject As Object
Dim RecipientObject As Object
Dim Email As String
Dim NS As NameSpace
Dim Folder As MAPIFolder
Set OlApp = CreateObject("Outlook.Application")
Set NS = ThisOutlookSession.Session
Set Folder = NS.PickFolder
For Each MailObject In Folder.Items
If MailObject.Class = olMail Then
For Each RecipientObject In MailObject.Recipients

Dim smtp As String
' Debug.Print "ad=", RecipientObject.Address

Select Case RecipientObject.AddressEntry.AddressEntryUserType
Case OlAddressEntryUserType.olExchangeUserAddressEntry
Set oEU = RecipientObject.AddressEntry.GetExchangeUser
If Not (oEU Is Nothing) Then
smtp = oEU.PrimarySmtpAddress
End If
Case OlAddressEntryUserType.olExchangeDistributionListAddressEntry
Set oEDL = RecipientObject.AddressEntry.GetExchangeDistributionList
If Not (oEDL Is Nothing) Then
smtp = oEDL.PrimarySmtpAddress
End If
End Select

Debug.Print smtp

Next
End If
Next
Set OlApp = Nothing
Set MailObject = Nothing
Set RecipientObject = Nothing
End Sub

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s