none
No-Fuzz copy of contact from email sender or recepient to contacts folder RRS feed

  • Question

  • If I get an email from a sender with a number of destinations, I can rather easily right the cender or one of the destinations, chose "add to contacts"  and then "move to folder" selecting a contact folder.

    This seems almoset inpossible to do in vba. Why is it possible in the user interface and not in code and how would be able to get a vba code as close to the pseudocode below.

    I allready have ideas answers of course:

    - I need to convert the sender and recepients objetts to contact items, but I'd rather not loop through and analyze each
      property

    - for inhouse contacts I somehow need to find the correponding exchange contacts and copy those to the contact folder
      but then I need to convertan exchange contact to a contact item (also seems impossible

    - I need a lightly more efficient alternative to find


    The pseudo code would be 

    mycontacts is a local contacts folder

    myemail is the email item I'm currently working with

    if mycontacts.find("[name] = "+chr(34)+myemail.sender.name + chr(34)) is nothing then _

          mymail.sender.copy mycontacts

    for each c in myemail.recepients

    if mycontacts.find("[name] = "+chr(34)+c.name + chr(34)) is nothing then _

    c.copy mycontacts

    next


    R

    Monday, January 20, 2014 1:09 PM

Answers

  • Hello Ragnar,

    The MailItem class from the Outlook object model provides the following properties for getting info about the sender:

    Sender

    SenderEmailAddress

    SenderEmailType

    SenderName

    For example, I have found the following code in MSDN:

    Public Sub DisplaySenderDetails() 
     Dim Explorer As Outlook.Explorer 
     Dim CurrentItem As Object 
     Dim Sender As Outlook.AddressEntry 
     Dim Contact As Outlook.ContactItem 
     
     Set Explorer = Application.ActiveExplorer 
     
     ' Check whether any item is selected in the current folder. 
     If Explorer.Selection.Count Then 
     
     ' Get the first selected item. 
     Set CurrentItem = Explorer.Selection(1) 
     
     ' Check for the type of the selected item as only the 
     ' MailItem object has the Sender property. 
     If CurrentItem.Class = olMail Then 
     Set Sender = CurrentItem.Sender 
     
     ' There is no sender if the item has not been sent yet. 
     If Sender Is Nothing Then 
     MsgBox "There's no sender for the current email", vbInformation 
     Exit Sub 
     End If 
     
     Set Contact = Sender.GetContact 
     
     If Not Contact Is Nothing Then 
     ' The sender is stored in the contacts folder, 
     ' so the contact item can be displayed. 
     Contact.Display 
     
     Else 
     ' If the contact cannot be found, display the 
     ' address entry in the properties dialog box. 
     Sender.Details 0 
     End If 
     End If 
     End If 
    End Sub
    

    It shows how to display the details of the sender of an e-mail. If the sender corresponds to a contact in the user's Outlook Contacts Address Book (CAB), the code example displays information about that contact in an inspector. If the sender is not a contact in the user's CAB, the code example displays details from the user's address entry (taken from the transport provider's address book container) in a dialog box.

    As you may see, there is no need to search the address book for a contact. The GetContact method returns Nothing if the AddressEntry object does not correspond to a contact in a Contacts Address Book. But the AddressEntry class doesn't provide any property or method for saving its instance as a contact. Instead, you need to create a corresponding contact in the address book manually. You can read more about this in the How To: Create a new Outlook Contact item programmatically article.

    Also you may be interested in the GetExchangeDistributionList and GetExchangeUser methods of the AddressEntry class when connecting to the exchange server.

    Monday, January 20, 2014 2:52 PM

All replies

  • Hello Ragnar,

    The MailItem class from the Outlook object model provides the following properties for getting info about the sender:

    Sender

    SenderEmailAddress

    SenderEmailType

    SenderName

    For example, I have found the following code in MSDN:

    Public Sub DisplaySenderDetails() 
     Dim Explorer As Outlook.Explorer 
     Dim CurrentItem As Object 
     Dim Sender As Outlook.AddressEntry 
     Dim Contact As Outlook.ContactItem 
     
     Set Explorer = Application.ActiveExplorer 
     
     ' Check whether any item is selected in the current folder. 
     If Explorer.Selection.Count Then 
     
     ' Get the first selected item. 
     Set CurrentItem = Explorer.Selection(1) 
     
     ' Check for the type of the selected item as only the 
     ' MailItem object has the Sender property. 
     If CurrentItem.Class = olMail Then 
     Set Sender = CurrentItem.Sender 
     
     ' There is no sender if the item has not been sent yet. 
     If Sender Is Nothing Then 
     MsgBox "There's no sender for the current email", vbInformation 
     Exit Sub 
     End If 
     
     Set Contact = Sender.GetContact 
     
     If Not Contact Is Nothing Then 
     ' The sender is stored in the contacts folder, 
     ' so the contact item can be displayed. 
     Contact.Display 
     
     Else 
     ' If the contact cannot be found, display the 
     ' address entry in the properties dialog box. 
     Sender.Details 0 
     End If 
     End If 
     End If 
    End Sub
    

    It shows how to display the details of the sender of an e-mail. If the sender corresponds to a contact in the user's Outlook Contacts Address Book (CAB), the code example displays information about that contact in an inspector. If the sender is not a contact in the user's CAB, the code example displays details from the user's address entry (taken from the transport provider's address book container) in a dialog box.

    As you may see, there is no need to search the address book for a contact. The GetContact method returns Nothing if the AddressEntry object does not correspond to a contact in a Contacts Address Book. But the AddressEntry class doesn't provide any property or method for saving its instance as a contact. Instead, you need to create a corresponding contact in the address book manually. You can read more about this in the How To: Create a new Outlook Contact item programmatically article.

    Also you may be interested in the GetExchangeDistributionList and GetExchangeUser methods of the AddressEntry class when connecting to the exchange server.

    Monday, January 20, 2014 2:52 PM
  • Hi Eugene,

    What first seemed to ba a copy of all the stuff I allready read i different forums actually contained the missing method catalyzer which was the method getcontact which I tried and which worked very well.

    Some questions remain however. Will I be able to get all the xchange info about the contact in this way from my company's exchange server, didn't chech if the getcontact gets the infor from the exchange serve or just creates a contact from the amail information.

    Thanks



    R

    Monday, January 27, 2014 5:07 PM
  • Hi Ragnar,

    The AddressEntry class provides the GetExchangeUser method which returns an ExchangeUser object that represents the AddressEntry if the AddressEntry  belongs to an Exchange AddressList object such as the Global Address List (GAL) and  corresponds to an Exchange user.

    Monday, January 27, 2014 5:28 PM