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
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
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/lib rary/offic e/ff864192 .aspx
AddressEntries.Item Method (Outlook)
https://msdn.microsoft.com
ASKER
here is the code for you guys should have mentioned it is from global address lists
Hope this provides some insight
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
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
to (eliminating the On Err "ErrName" block completely)
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
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
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
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
ASKER
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
ASKER
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(auditorNam
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?
ASKER
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.
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.
ASKER
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
could I add to or modify the .Address properties to return a different value
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window
»bp