Solved

Macro to Add Contacts based on Names in the To Line

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Resolve DNS query failed errors for Exchange
Large Outlook files lead to various unwanted errors and corruption issues. Furthermore, large outlook files can also make Outlook take longer to start-up, search, navigate, and shut-down. So, In this article, i will discuss a method to make your Out…
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: …
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…

895 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

17 Experts available now in Live!

Get 1:1 Help Now