Consulting

Results 1 to 9 of 9

Thread: VBA code to create Outlook contacts from e-mail recipients - non-empty properties

  1. #1
    VBAX Regular
    Joined
    Apr 2010
    Posts
    19
    Location

    VBA code to create Outlook contacts from e-mail recipients - non-empty properties

    Hello all,

    I would like to ask you if I can find on this forum a VBA code (I am using Outlook 2010) that is able to quickly create Outlook contacts, using a saved e-mail message, that contains hundred of e-mail recipients (all of them are stored on To field).

    In other words, it is in fact equivalent to right click on each recipient, and then "Add to Outlook Contacts", but applied to ALL recipients from TO field.

    It is very important to mention that VBA code should "import" from each and every recipient not only the name, and the e-mail address (basic information), but it should "import" all fields that are not empty. Some recipients do contain home phone numbers, some of them don't, so in order to make sure everything is collected, all non-empty fields should be considered.

    If such a code is not available on this forum, can someone help ?

    Also, please indicate which "References" I should activate on Outlook VBA editor.

    Thank you a lot,
    Romulus.

    P.S. I have tried several days to find anything close to that on various Web sites, but I was not lucky

  2. #2
    If I understand the requirement correctly, you want to extract the recipients from the TO field of an e-mail message to the Outlook contacts. So far so good, but the TO field does not carry the supplementary information required to fill the fields, only the e-mail data. Where do you propose the other information comes from?
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Apr 2010
    Posts
    19
    Location
    Hello Mr. Mayor,

    Thank you a lot for your feedback. Your understanding is correct, I guess my definition of requirement was not entirely correct.

    Let me rephrase it a little bit: let us imagine right click on any name from TO field and the command "Add to Outlook Contacts". If we do so, Outlook creates the contact corresponding to that e-mail name, displays the whole contact page with many fields filled in with data. The user can choose "Save & Close", and contact window disappears.

    So far, so good, but this is done for 1 e-mail name, only. As I wrote initially, I do have hundreds of those e-mail names in TO fields. In order to create those contacts for all those hundred of names, I have to right click each and every e-mail name, I might miss some of the names, and some other risks.


    This is the reason why I raised this topic about a VBA code that can achieve this multiple task in an elegant and automated way.

    One more thing, to better clarify my request: if you right click any name in TO field, there is an option called "Contact Card", if we click on it, and then if we choose the list of options (on the upper, right hand side), one of the options is called "Outlook Properties", see below picture (I covered all personal information):

    Outlook properties.jpg

    What I need is to copy all those fields contents, from all 5 tabs: General, Organization, Phone/Notes, Member of, and E-mail addresses, into fields of the Contact item, for all those e-mail names from TO field. Some of these "Outlook Properties" fields might be empty, but it does not matter, if a certain field is empty, the corresponding field from Contacts item will be empty, as well, no worries.

    In a nutshell, my need is something like select all items from TO field, right click, and then use "Add ALL to Outlook Contacts", but there is no such a thing in Outlook 2010, for all e-mail names.

    Can you create this VBA code, able to execute this "Add ALL to Outlook contacts" ?

    Thank you once again, and I am looking forward to receiving good news from you :-).

    Regards,
    Romulus.

  4. #4
    I think you are destined for disappointment. The only information associated with the recipients of an e-mail message is the name and address of each recipient. You can create contacts entries from that information (see below), but the other fields are not available.

    Sub CreateContactsFromMail()
    Dim olFolder As Outlook.Folder
    Dim olNS As Outlook.NameSpace
    Dim olItem As ContactItem
    Dim sRecip As String
    Dim oRecipient As Recipient
    Dim oMail As Outlook.MailItem
        Set olNS = Application.GetNamespace("MAPI")
        Set olFolder = olNS.GetDefaultFolder(olFolderContacts)
    
        Set oMail = Application.ActiveExplorer.Selection.Item(1)
        For Each oRecipient In oMail.Recipients
            Set olItem = olFolder.Items.Add(olContactItem)
            With olItem
                .Email1Address = oRecipient.Address
                .Email1DisplayName = oRecipient.Name
                .Email1AddressType = oMail.SenderEmailType
                .FullName = oRecipient.Name
                .Save
            End With
            DoEvents
        Next oRecipient
    
    lbl_Exit:
        Set olFolder = Nothing
        Set olItem = Nothing
        Set olFolder = Nothing
        Set olNS = Nothing
        Set oRecipient = Nothing
        Set oMail = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    VBAX Regular
    Joined
    Apr 2010
    Posts
    19
    Location
    Hello again,

    Thank you for code you sent. As I mentioned within my first reply, my requirement was in fact about a VBA code capable of 100% reproduction of "Add to Outlook Contacts" command, after a right click made on an e-mail name, but not just on one name, but on all e-mail names from TO field.


    Can this maybe be achieved by using "OutlookSpy" add-in ? I have installed it, but I am not familiar with MAPI environment, so I do not know how to use it. Thank you once again.

    Regards,
    Romulus.
    Last edited by Romulus; 05-28-2016 at 07:49 AM.

  6. #6
    You cannot magically produce the information for the missing fields which is not contained in the message.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  7. #7
    VBAX Regular
    Joined
    Apr 2010
    Posts
    19
    Location
    Hello again,

    Of course, I never expected a magical solution. I am just trying to find out whether a VBA code can be created, equivalent to "Add to Outlook Contacts".

    Have you maybe seen the question I raised about OutlookSpy ?

    Cheers,
    Romulus.

  8. #8
    I don't know anything about Outlook Spy and I have already provided a macro that will create contacts entries for all the recipients of a message.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  9. #9
    VBAX Newbie
    Joined
    Nov 2023
    Posts
    1
    Location
    Hi gmayor.
    I came across your above script which is excellent and simple.
    I wonder what should I add in it so that if the contact already exist, the script does not create a duplicate and goes to Next message ?.
    Thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •