Link to home
Start Free TrialLog in
Avatar of Frank Dom
Frank Dom

asked on

Get First Email from AddressEntries.Address

My current problem is that when using the .Address function I get returned an address that isn't the email address I want but the last entry in the email addresses list. Is there any way for me to specify which address i want

an example of the contact's email address list would be

SIP:johndoe@email.com
smpt:johndoe@email.com
etc.
0xf218Hi=There

and it would return the 0xf218Hi=There when using .address
Avatar of Bill Prew
Bill Prew

Have you tried using the Item() method?

Set myNameSpace = Application.GetNamespace("MAPI")
Set myAddressList = myNameSpace.AddressLists("Contacts")
Set myAddressEntries = myAddressList.AddressEntries
Set myAddress = myAddressEntries.Item(1).Address

Open in new window


»bp
Always a good idea to include the code snippet being used including which AddressEntries being interrogated (i.e. from an AddressList or Recipients collection etc). The index property is either a numeric or string value

AddressEntries.Item Method (Outlook)
https://msdn.microsoft.com/en-us/library/office/ff864192.aspx
Avatar of Frank Dom

ASKER

here is the code for you guys should have mentioned it is from global address lists

Sub GetEmail()
    Dim oApp
    Dim oNameSpace
    Dim GAL
    Dim auditorName As String
    Dim auditSheet As workSheet
    Dim auditWork As Workbook
    Dim xlApp
    Dim workbookPath As String
    Dim workSheet As String
    Dim i As Integer
    Dim splitName() As String
    Dim catchError As String
    Dim email As String
    Dim address
    
    Set oApp = Outlook.Application
    Set oNameSpace = oApp.GetNamespace("MAPI")
    Set GAL = oNameSpace.AddressLists("Offline Global Address List")
    Set xlApp = CreateObject("Excel.Application")
    
    workbookPath = "C:\Users\Admin\Desktop\Records.xlsm"
    i = 1
    
    Set auditWork = Workbooks.Open(workbookPath, , False, , , True, , , , False)
    Set auditorSheet = auditWork.Sheets("Auditor email")
    
    auditWork.Activate
    
Begin:
    Do While auditorSheet.Cells(i, 1).Value <> ""
        On Error GoTo ErrHandler:
            splitName = Split(auditorSheet.Cells(i, 1))
            Dim splitLength As Integer
            splitLength = UBound(splitName) + 1
            If splitLength <= 3 Then
            catchError = splitName(2)
            Else
            auditorName = splitName(0) & " " & splitName(1) & " " & splitName(2)
            End If
SearchContinue:
        On Error GoTo ErrName:
            email = GAL.AddressEntries(auditorName).item(1).address
            
            auditorSheet.Cells(i, 2).Value = email
    Loop
    
    auditWork.Close (True)
    
    Set oApp = Nothing
    Set oNameSpace = Nothing
    Set GAL = Nothing
    Set xlApp = Nothing
    
Exit Sub

ErrHandler:

    auditorName = auditorSheet.Cells(i, 1).Value
    
    Resume SearchContinue
Exit Sub

ErrName:
    auditorSheet.Cells(i, 2).Value = "Error 404"
    
    i = i + 1
    
    Resume Begin
End Sub

Open in new window


Hope this provides some insight
An unrelated comment/question is why you're not using early binding. That aside

Don't completely follow the various On Error tiers. Just focusing on the "ErrName" portion - personally would change the following from

 On Error GoTo ErrName:
            email = GAL.AddressEntries(auditorName).item(1).address
            

Open in new window


to (eliminating the On Err "ErrName" block completely)

Dim AddrEntry as AddressEntry

    set AddrEntry = GAL.AddressEntries(auditorName)
    if AddrEntry is nothing then
        auditorSheet.Cells(i, 2).Value = "Error 404"  
    else
        email = AddrEntry.Address
        auditorSheet.Cells(i, 2).Value = email
   endif
   i = i + 1
Loop

Open in new window


Also, (1) your Do While loop would become infinite since var "i" only gets incremented if "On Error" gets triggered - (2) no need to Dim "splitlength" inside the Do While Loop
Yes, those things are all well and good, but that doesn't solve the problem. I made those changes before but testing the macro makes outlook and excel buggy when stopped so many changes weren't saved. The underlying problem still exists because the Item() method doesn't work so I still can't get the first address from the .address
I made those changes before

Not sure what means exactly and not going to try and guess what changes you're referring to. Can only go by what you provided.

As for things being "buggy...............so many changes weren't saved" -  also not sure what you're referring to but as mentioned, your "Do While" loop in and of itself is an infinite loop unless an error is explicitly encountered and don't understand the first ON Error which breaks out of the Loop so on that note will have to leave this to someone else to go through your entire routine as provided
I realize those errors and made the necessary changes but my initial problem is getting the right address from Global Address List when there are multiple addresses as provided in the question, but still thank you for your help
my initial problem is getting the right address from Global Address List when there are multiple addresses as provided in the question

Just to clarify one last item -  by "multiple addresses" are you referring to alias (or proxy) addresses that exist for a single AddressEntry returned by (AddressEntries(auditorName)......)? The Address property would return the EX email address so a little puzzled as to why that wasn't on the list along with a value of "0xf218Hi=There" getting returned which makes no sense.

If you interrogate the "Global Address List" (or any of the sub-lists like "All Users" or "All Contacts" as opposed to the "Offline Global Address List" - do you get the same results?
here is the UX which is where I am getting my information
User generated image
And same result using global address list
Those are the addresses as found for <EX Proxy addresses> whereas the .Address field returns the internal EX (X500) address (at least in every test I've done).  That said, not clear as to why you're getting a value of <0xf218Hi=There> returned (unless that was a typo) but admittedly, I didn't use Excel VBA to test the results using the Outlook Object Model / AddressEntry.Address property

I would suspect that a more accurate description of your requirement is not that you want to retrieve the first address in the list but rather that you want to retrieve the SMTP address for the given User/Contact for which there's no OOM AddressEntry property.
the 0xf228Hi=THere was an example of the X500 just an example of the type I didn't want,  Yes but that is correct I am trying to get that address based off the given user and not the X500 that I am getting, Do you know of a way to get that address by any chance, or do you believe it is not possible

could I add to or modify the .Address properties to return a different value
ASKER CERTIFIED SOLUTION
Avatar of Karl Timmermans (Outlook MVP 2012-2018)
Karl Timmermans (Outlook MVP 2012-2018)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial