How Can I Search The Outlook (2010) Global Address List For A Name?
Solution 1:
Problem solved!
Thanks to Dmitry'sanswers I could produce this minimal Python code which demonstrates what I wanted to achieve:
from __future__ import print_function
import win32com.client
search_string = 'Doe John'
outlook = win32com.client.gencache.EnsureDispatch('Outlook.Application')
recipient = outlook.Session.CreateRecipient(search_string)
recipient.Resolve()
print('Resolved OK: ', recipient.Resolved)
print('Is it a sendable? (address): ', recipient.Sendable)
print('Name: ', recipient.Name)
ae = recipient.AddressEntry
email_address = Noneif'EX' == ae.Type:
eu = ae.GetExchangeUser()
email_address = eu.PrimarySmtpAddress
if'SMTP' == ae.Type:
email_address = ae.Address
print('Email address: ', email_address)
Solution 2:
Your code above deals with the contacts in the default Contacts folder. If you want to check if a given name is in Outlook (either as a contact or in GAL), simply call Application.Session.CreateRecipient
followed by Recipient.Resolve
. If the call returns true
, you can read the Recipient.Address
and various other properties.
Solution 3:
The method in @Prof.Falken's solution doesn't always work when there are multiple matches for the search string. I found another solution, which is more robust as it uses exact match of the displayname
.
It's inspired by How to fetch exact match of addressEntry object from GAL (Global Address List).
import win32com.client
search_string = 'Doe John'
outlook = win32com.client.gencache.EnsureDispatch('Outlook.Application')
gal = outlook.Session.GetGlobalAddressList()
entries = gal.AddressEntries
ae = entries[search_string]
email_address = Noneif'EX' == ae.Type:
eu = ae.GetExchangeUser()
email_address = eu.PrimarySmtpAddress
if'SMTP' == ae.Type:
email_address = ae.Address
print('Email address: ', email_address)
Post a Comment for "How Can I Search The Outlook (2010) Global Address List For A Name?"