'Format of email address from Outlook (EX instead of SMTP)

I have a list of first and last names in Excel and I want to utilize that list to look up email address in Outlook using visual basic.

I'm using the following VB code:

    Private Sub GetAddresses()
    Dim o, AddressList, AddressEntry
    Dim c As Range, r As Range, AddressName As String
    Set o = CreateObject("Outlook.Application")
    Set AddressList = o.Session.AddressLists("Global Address List")
    Set r = Range("a1:a3")
    For Each c In r
        AddressName = Trim(c.Value) & ", " & Trim(c.Offset(0, 1).Value)
        For Each AddressEntry In AddressList.AddressEntries
            If AddressEntry.Name = AddressName Then
                c.Offset(0, 2).Value = AddressEntry.Address
                Exit For
            End If
        Next AddressEntry
    Next c
    End Sub

The code seems to be working fine up until the point of actually retrieving the email address. After it matches a name its returning the following instead of the address. Does anyone have an idea of what I'm doing wrong.

/O=Compnay/OU=Company/cn=Recipients/cn=shs

Thanks in advance for you help.



Solution 1:[1]

That looks like a perfectly valid address of type EX (as opposed to SMTP). Use AddressEntry.GetExchangeUser().PrimarySmtpAddress to retrieve the SMTP address.

Do not loop through all items in an address list that can potentially contains tens of thousands of entries. Use Aplication.Sesssion.CreateRecipient, then call Recipient.Resolve. If successful, you can retrieve the AddressEntry object from Recipient.AddressEntry.

If you need to make sure the name is resolved against GAL only (by the way, you should not hardcode the GAL name, it will differ based on locale), you can use Redemption (I am its author) and its AddreessList.ResolveName method - all you need to do is call RDOSession.AddressBook.GAL.ResolveName

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1