'Search for an ambiguous outlook recipient with python

I am trying to use python to search for an outlook recipient using this code:

import win32com.client

search_string = 'name'
outlook = win32com.client.gencache.EnsureDispatch('Outlook.Application')
recipient = outlook.Session.CreateRecipient(search_string)
recipient.Resolve()
print('Resolved OK:', recipient.Resolved)
print('Name: ' , recipient.Name)
ae = recipient.AddressEntry
email.address = None

if 'EX' == ae.Type:
    eu = ae.GetExchangeUser()
    email_address = eu.PrimarySmtpAddress

if 'SMTP' == ae.Type:
    email_address = ae.Address

print('Email Address: ' , email_address)

The problem is when the search returns more then one result i cannot resolve the name, I would like to receive a list of the results which the user can choose from in case there is more then one result how do i do that?



Solution 1:[1]

Outlook Object Model won't let you search for multiple matches: it either finds a unique match, or returns an error if not matches are found or if more than one march exists.

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.

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.

In VB script:

  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 "------------"

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