View Full Version : VBA code to create Outlook contacts from e-mail recipients - non-empty properties
Romulus
05-27-2016, 10:41 AM
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 :(
gmayor
05-27-2016, 09:35 PM
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?
Romulus
05-28-2016, 02:02 AM
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):
16281
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.
gmayor
05-28-2016, 04:00 AM
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
Romulus
05-28-2016, 07:00 AM
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.
gmayor
05-28-2016, 08:26 PM
You cannot magically produce the information for the missing fields which is not contained in the message.
Romulus
05-28-2016, 10:51 PM
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.
gmayor
05-29-2016, 12:21 AM
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.
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
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.