Solved

Macro to Add Contacts based on Names in the To Line

Posted on 2012-03-21
5
583 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
  • 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
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 …
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now