'Find all email addresses corresponding to an ambiguous name
User has a column with names (or name, surname) to send email.
When there are several with the same name and surname the macro is selecting the first matching.
How can I check if for a specific name there is more than one email addresse, to perform another function?
My code to get email address.
Dim myolApp As Outlook.Application
Dim myNameSpace As Namespace
Dim myAddrList As AddressList
Dim myAddrEntry As AddressEntry
Dim AliasName As String
Dim c As Range
Dim EndRow As Integer, n As Integer
Dim exchUser As Outlook.ExchangeUser
Set myolApp = CreateObject("Outlook.Application")
Set myNameSpace = myolApp.GetNamespace("MAPI")
Set myAddrList = myNameSpace.AddressLists("Global Address List")
EndRow = Cells(Rows.Count, 1).End(xlUp).Row
For Each c In Range("A1:A" & CStr(EndRow))
AliasName = LCase(Trim(c))
c = AliasName
Set myAddrEntry = myAddrList.AddressEntries(AliasName)
Set exchUser = myAddrEntry.GetExchangeUser
On Error Resume Next
c.Offset(0, 4) = exchUser.PrimarySmtpAddress
Next c
End Sub
Solution 1:[1]
You can use formula in Excel sheet =COUNTIF(column_range, name_surname) and then you can read values with your VBA code. When result of formula is >1 then you do some other procedure.
Hopefully all your names are sorted in some way first name and then surname or something, sometimes happens that they are mixed and then this aproach might not work correctly.
Solution 2:[2]
There nothing really in Outlook Object Model that would help you with ambiguous names. You can of course loop through all items in GAL (Namespace.GetGlobalAddressList
), but large GAL containers would not even let you loop through its entries; and if you can loop, it can take a very long time (as in tens of minutes).
Extended MAPI (C++ or Delphi) lets you either retrieve the list of ambiguous names or display the dialog box that lets you select one entry from the list of ambiguous names. If VBA is your only option, you can use Redemption (I am its author) which exposes that MAPI functionality:
set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = myolApp.Session.MAPIOBJECT
'silently retrieve the list matches
set AdrrEntries = Session.AddressBook.GAL.ResolveNameEx("john")
MsgBox AdrrEntries.Count & " names were returned by ResolveNameEx:"
for each AE in AdrrEntries
MsgBox AE.Name
next
'allow Outlook to display the ambiguous name dialog if necessary
set Match = Session.AddressBook.ResolveName("john", true)
if not (match is Nothing) Then
MsgBox match.Name
End If
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 | R35P3K7 |
Solution 2 |