Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting the first entry of multiple entries when resolving an Outlook recipient

Tags:

excel

vba

outlook

I have a function that takes a name and resolves it in Outlook to return the alias for the selected name. This fails when there is more than one entry for the selected name in the address book. e.g. "Smith, Bob" & "Smith, Bob X". If the name I try to resolve is "Smith, Bob X", the code works fine, but plain "Smith, Bob" fails.

I presume when multiple entries are found, Outlook opens the Check Names dialogue (this occurs when I manually resolve names).

When my code finds multiple entries, how do I select the first one?

Function GETTPX(ByVal UserName As String) As String
Dim objOL As Object
Dim oRecip As Outlook.Recipient
Dim oEU As Outlook.ExchangeUser
Dim oEDL As Outlook.ExchangeDistributionList

Set objOL = CreateObject("Outlook.Application")

Set oRecip = objOL.Session.CreateRecipient(UserName)
oRecip.Resolve
If oRecip.Resolved Then
    Set oEU = oRecip.AddressEntry.GetExchangeUser
End If
GETTPX = oEU.Alias

Set oRecip = Nothing
Set objOL = Nothing

End Function
like image 339
Steven Walker Avatar asked Feb 05 '16 09:02

Steven Walker


1 Answers

On the Extended MAPI level (C++ or Delphi only) you can create a PR_ANR restriction on the contents table of a particular search container (such as GAL). That is what Outlook does when it resolves a name you typed in the To edit box - it goes through all containers in the search path and applies the PR_ANR restriction. If there are multiple matches found, it displays a dialog box with the list. If there is a single match, it is returned and the search is stopped, otherwise it continues to the next container in the search path.

Outlook Object Model however does not expose this functionality. If using Redemption (I am its author - any language) is an option, it exposes RDOAddressBook.ResolveNameEx and RDOAddressList.ResolveNameEx, which return a list of matches.

  set Session = CreateObject("Redemption.RDOSession")
  Session.MAPIOBJECT = Application.Session.MAPIOBJECT
  set AdrrEntries = Session.AddressBook.ResolveNameEx("john")
  Debug.Print AdrrEntries.Count & " names were retruned by ResolveNameEx:"
  Debug.Print "------------"
  for each AE in AdrrEntries
    Debug.Print AE.Name
  next
  Debug.Print "------------"
like image 151
Dmitry Streblechenko Avatar answered Sep 28 '22 05:09

Dmitry Streblechenko