?
Solved

Macro to Add Contacts based on Names in the To Line

Posted on 2012-03-21
5
Medium Priority
?
632 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

When you have clients or friends from around the world, it becomes a challenge to arrange a meeting or effectively manage your time. This is where Outlook's capability to show 2 time zones in one calendar comes in handy.
Changing a few Outlook Options can help keep you organized!
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Suggested Courses

762 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