Create Contacts From Messages

Last reviewed on January 5, 2013

This macro will go through the messages in a folder and create a contact for each sender

A user needed to solve this problem:

My contact list on a prior system was deleted. I imported all of my emails into my new Microsoft Outlook 2003 but going through all the emails individually to add contacts is horrible when you have thousands of emails. Does anyone have the micro basic code to set up a macro to add contacts from the personal favorite folder?

The user is right – its a slow, painful process to select each message, right click and choose Add to contacts. This macro solves the problem by automating the process – it goes through each message in the selected folder and creates a Contact for the sender.

For utilities that can do this or if you want to add the addresses of all people you send messages to (either new messages or replies) see Add addresses automatically to Microsoft Outlook Contacts.

This macro will work in any folder, on all selected items in that folder.

Any selected mail items will be harvested. Because Ken wanted to only use the OOM and so it’s not restricted to 2007 or later only, he didn’t use the MAPI properties for SentOnBehalfOf* other than for name. So the email addresses may not be accurate (for example mails sent to a mailing list will often show as sent by the list address, not the actual sender.)

To use it, copy the code to Outlook’s VB editor. If you need to run it on several folders, add a toolbar button for it. Select the messages you want to create contacts from and run it.

Tip: it might be helpful to copy messages from sender’s whose address you want to save to a new folder then run it on that folder. This will allow you to peruse the messages and select just the sender’s whose address you need saved.

VBA to walk a folder and create contacts from all senders

This macro is compliments of Outlook MVP and developer Ken Slovak from http://www.slovaktech.com

Ken adds: “If this were 2007 or later code only I’d use PropertyAccessor to pick up the PR_SENT_REPRESENTING_ADDRTYPE and PR_SENT_REPRESENTING_EMAIL_ADDRESS properties instead of using SenderEmailAddress and SenderEmailAddressType.”


' The AddAddressesToContacts procedure can go in any Module
' Select the mail folder and any items to add to contacts, then run the macro

Public Sub AddAddressesToContacts()
Dim folContacts As Outlook.MAPIFolder
Dim colItems As Outlook.Items
Dim oContact As Outlook.ContactItem
Dim oMail As Outlook.MailItem
Dim obj As Object
Dim oNS As Outlook.NameSpace

Dim response As VbMsgBoxResult

Dim bContinue As Boolean

Dim sSenderName As String

On Error Resume Next

Set oNS = Application.GetNamespace("MAPI")
Set folContacts= oNS.GetDefaultFolder(olFolderContacts)
Set colItems= folContacts.Items

For Each obj In Application.ActiveExplorer.Selection
If obj.Class = olMail Then
Set oContact= Nothing

bContinue= True
sSenderName= ""

Set oMail = obj

sSenderName = oMail.SentOnBehalfOfName
If sSenderName = ";" Then
sSenderName = oMail.SenderName
End If

Set oContact = colItems.Find("[FullName] = '" & sSenderName & "'")

If Not (oContact Is Nothing) Then
response = MsgBox("This appears to be an existing contact: " & sSenderName & ". Do you still want to add it as a new contact?", vbQuestion + vbYesNo, "Contact Adder")
If response = vbNo Then
bContinue = False
End If
End If

If bContinue Then
Set oContact = colItems.Add(olContactItem)
With oContact
.Body = oMail.Subject

.Email1Address = oMail.SenderEmailAddress
.Email1DisplayName = sSenderName
.Email1AddressType = oMail.SenderEmailType

.FullName = oMail.SenderName

.Save
End With
End If
End If
Next

Set folContacts = Nothing
Set colItems = Nothing
Set oContact = Nothing
Set oMail = Nothing
Set obj = Nothing
Set oNS = Nothing
End Sub

How to use VBA

Copy and paste the code from this page into your ThisOutlookSession project. To do this, click within the code, Select All using Ctrl+A, Ctrl+C to copy.

In Outlook, press Alt+F11 to open the VBA editor and expand Microsoft Outlook Objects then double click on ThisOutlookSession to open it in the editing pane and Ctrl+P to paste the code.

For more detailed instructions and screenshots, see How to use Outlook’s VBA Editor

Written by

Diane Poremsky
A Microsoft Outlook Most Valuable Professional (MVP) since 1999 and involved in IT support since 1985, Diane is the author of several books and video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.