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.
Create Contacts for All Senders in a Folder
This macro is compliments of Outlook MVP and developer Ken Slovak from https://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
More Information
How to use Outlook's VBA Editor
How to Create Macro in Visual Basic to add Contacts from Personal Folder - original post in our forums
Add addresses automatically to Microsoft Outlook Contacts - create contacts for people you send messages to.
How can I get it to Update or Merge a contact if it already exists? Yes I do have a need for this before anyone tells me I don't need this or shouldn't ;)
Outlook has an option to look for duplicates when a new contact is added - this won't merge the contacts there. There are utilities that will merge duplicate contacts - you could do it with a macro, but really need to display the data and choose which to keep - that is better as a com addin rather than a simple macro. I have a list of addins at https://www.slipstick.com/addins/duplicates-addins/remove-duplicate-contacts-from-outlook/ - Contacts Scrubber lets you pick the fields to keep. Others on the list may as well.
Great article. Is there any way of modifying the code so it transfers email addresses from the To, rather than Sent From items? I have many draft emails I would like to put into contacts without having to send an email.
Try .Email1Address = oMail.To Is there more than one address in the to field? if so, you need to use the recipient collection.
Hello,
How is it possible to add the adresses written in Sent to, CC & BCC too? So not just the sender, but basicly all the mailadresses.
Thx.
You could - you'd need to go through the recipient collection. Between the two end if's at the end, add this code. After the sender is added, it'll work on the recipients. (BTW, it won't work on the BCC field as you don't see who it was sent to. )
For Each Recipient In oMail.Recipients
sSenderName = Recipient.Name
' add recipients
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 = Recipient.Address
.Email1DisplayName = sSenderName
.FullName = sSenderName
.Save
End With
End If
Next Recipient
thanks for this, but i get a error: "next without for" Here is my code: 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 sSenderName = Recipient.Name ' add recipients… Read more »
that means there are too many end if, for's, next etc - it highlights on Next Recipient you either need a matching for or delete that line.
Fix the problem, i was missing the first line.. :-o
What i needed was only the recipients email adress so i changed
Set oContact = colItems.Find("[FullName] = '" & sSenderName & "'")
to:
Set oContact = colItems.Find("[Email1Address] = '" & sSenderName & "'")
works like a charm, Thanks a lot!
Hello everyone,
thank for this macro, it is very useful! I was wondering if instead of creating a new contacts in the address book, it was possible to export these informations directly into an excel file.
Many thanks!
Sure. See https://www.slipstick.com/developer/vba-copy-outlook-email-excel-workbook/ for sample code. It's just a matter of changing which fields you grab. The sample is for a run a script rule.
Many thanks for posting this code. I would like to change from the default contacts folder to a specific sub folder called Test. How would I change the code for this to work.
Many thanks
Colin
This line sets the folder:
Set folContacts= oNS.GetDefaultFolder(olFolderContacts)
to use a subfolder of contacts, use
Set folContacts= oNS.GetDefaultFolder(olFolderContacts).folders("folder name")
More on using non-default folders: https://www.slipstick.com/developer/working-vba-nondefault-outlook-folders/
@ Ilana Fridlin: Find this section in the code:
If Not (oContact Is Nothing) Then
(...)
End If
End If
And replace it with:
If Not (oContact Is Nothing) Then
bContinue = False
End If
Is there a way to add a comand so that when it says the contact already exists and asks if I want to still add, it it automatically "clicks" on NO and keeps on going? Cause it's really unproductive to have to keep clicking NO until it ends... Please...? Or somehow that it only copies those that don't already exist...
The dialog is not exposed to the object model, sorry. You would need to use a windows utility that responses to dialogs. ClickYes and others can do this.
You can also use a weight or a pencil to hold down the 'N' key while you run the script. I've done this before, it works well most of the time.
Hi, there's a typo on line:
response = MsgBox("This appears to be an existing contact: " & sSenderName" &
The double quotes after sSenderName" must be removed.
Peter
Thanks for finding that error.