Solved

Macro to Add Contacts based on Names in the To Line

Posted on 2012-03-21
5
621 Views
Last Modified: 2012-03-28
Hi Experts,

Could someone provide the VBA code that adds new contacts based on the names /contacts listed in the "To" line of an email?  

By the way, I have multiple names (over 100) listed on my To line.

I have seen some code on EE already but they seem to add contacts based on the name listed in sender.

Many Thanks,
RR
0
Comment
Question by:rav_rav
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 76

Expert Comment

by:David Lee
ID: 37765115
Hi, RR.

I can help with this.  Do you want the script to run automatically or manually?
0
 

Author Comment

by:rav_rav
ID: 37765760
Script to run manually.  Thanks
0
 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
ID: 37770583
Here's the code.  Follow these instructions to add it to Outlook.

1.  Start Outlook
2.  Press ALT+F11 to open the Visual Basic Editor
3.  If not already expanded, expand Microsoft Office Outlook Objects
4.  If not already expanded, expand Modules
5.  Select an existing module (e.g. Module1) by double-clicking on it or create a new module by right-clicking Modules and selecting Insert > Module.
6.  Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook's VB Editor window
7.  Edit the code as needed.  I included comments wherever something needs to or can change
8.  Click the diskette icon on the toolbar to save the changes
9.  Close the VB Editor

Sub HarvestAddresses()
    Const SCRIPT_NAME = "Harvest Addresses"
    Dim olkItem As Outlook.MailItem
    Select Case TypeName(Application.ActiveWindow)
        Case "Explorer"
            For Each olkItem In Application.ActiveExplorer.Selection
                ProcessMessage olkItem
            Next
        Case "Inspector"
            ProcessMessage Application.ActiveInspector.CurrentItem
    End Select
    MsgBox "Finished", vbInformation + vbOKOnly, SCRIPT_NAME
End Sub

Sub ProcessMessage(olkMail As Outlook.MailItem)
    Dim olkMail As Outlook.MailItem, _
        olkRpnt As Outlook.Recipient, _
        olkCont As Outlook.ContactItem
    For Each olkRpnt In olkMail.Recipients
        Set olkCont = GetContact(olkRpnt.Address)
        If TypeName(olkCont) = "Nothing" Then
            Set olkCont = Application.CreateItem(olContactItem)
            With olkCont
                .Email1Address = olkRpnt.Address
                .FullName = olkRpnt.Name
                .Save
            End With
        End If
    Next
    Set olkMail = Nothing
    Set olkRpnt = Nothing
    Set olkCont = Nothing
End Sub

Function GetContact(strAddress As String) As Outlook.ContactItem
    Dim olkContacts As Outlook.Items, olkContact As Outlook.ContactItem
    Set olkContacts = Session.GetDefaultFolder(olFolderContacts).Items
    Set olkContact = olkContacts.Find("[Email1Address]='" + strAddress + "'" + " OR [Email2Address]='" + strAddress + "'" + " OR [Email3Address]='" + strAddress + "'")
    Set GetContact = olkContact
End Function

Open in new window


To use this solution

1.  Select one or more emails or open an email.
2.  Run the macro.

The macro loops through each email's addressees.  For each one it searches Contacts to see if a contact with that address already exists.  If it does not find one, then it adds a contact.  If one is found, then it does nothing.
0
 

Author Closing Comment

by:rav_rav
ID: 37776576
Great; thanks for your help.
0
 
LVL 76

Expert Comment

by:David Lee
ID: 37776603
You're welcome!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Find out what you should include to make the best professional email signature for your organization.
This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question