0

I use the following code to get ContactInfo (in Outlook2010) of each recipient of a mail to be sent. The code works, but only for a few contacts, although all contacts are stored in my adressbook. For some the last line (GetContact) delivers Nothing. Why?

' Create RDO session Dim session Set session = CreateObject("Redemption.RDOSession")

Set session.MAPIOBJECT = Application.session.MAPIOBJECT

' Get current email

ActiveInspector.CurrentItem.Save ' Necessary to get current status
Dim mail
Set mail = session.GetMessageFromID(ActiveInspector.CurrentItem.EntryID)

' Create salutation line
Dim salutationLine As String
salutationLine = ""

For Each Recipient In mail.Recipients
    ' Skip CC and BCC addresses
    If (Recipient.Type <> olTo) Then GoTo NextRecipient

    ' Assume standard salutation and use complete name as first name
    Dim salutationType As String
    salutationType = ""
    Dim firstName As String
    Dim lastName As String
    Dim recipientName As String

    recipientName = IIf(Recipient.Name <> "", Recipient.Name, Recipient.Address)
    lastName = ""

    If InStr(1, recipientName, " ") > 0 Then
        firstName = Split(recipientName, " ")(0)
        lastName = Split(recipientName, " ")(1)
    End If
    Dim addressEntry
    Set addressEntry = Recipient.addressEntry
    If (Not addressEntry Is Nothing) Then
        ' If we have qualified name information: extract first and last name
        If (addressEntry.firstName <> "") Then firstName = addressEntry.firstName
        If (addressEntry.lastName <> "") Then lastName = addressEntry.lastName

        Dim contactInfo
        Set contactInfo = addressEntry.GetContact()



        If (Not contactInfo Is Nothing) Then
HHeckner
  • 4,722
  • 4
  • 23
  • 33

1 Answers1

1

GetContact in both Outlook Object Model and Redemption relies on the entry id being of OAB type. On the incoming messages, all SMTP recipients have one-off entry id (it does not point to any existing address book objects and embeds name, address and address type inside).

In general, you will need to extract the recipient address, then search the Contacts folder for a matching contact based on email1, email2 or email3 values.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • Sorry for being unprecise. My code above tries to do this on new email about to be sent out. So we are not talking about incoming emails but about outgoiing emails – HHeckner Oct 21 '13 at 17:26
  • You can still get a one-off entry id if you manually typed the address or used autocomplete instead of clicking the To button and selecting one of the contacts. – Dmitry Streblechenko Oct 21 '13 at 18:43