如何在Outlook(2010)全球地址列表中搜索名称? [英] How can I search the Outlook (2010) Global Address List for a name?
问题描述
我有一个名字列表,其中一些完整,一些被截断.我想在Outlook地址列表中搜索这些名称的匹配项.
I have a list of names, some of them complete, some truncated. I would like to search the Outlook address list for matches for these names.
最接近的是此Python代码出现了从ActiveState代码中获取,但它不搜索全局地址,仅搜索我的(本地?)列表,该列表中有3个地址,这显然是不对的.应该有成千上万的记录.
The closest I have come is this Python code which came from ActiveState Code, but it does not search the global addresses, only my (local?) list, which has 3 addresses in it, which is obviously not right. There should be thousands of records.
任何提示都欢迎.我已经用Google搜索并阅读了数十页,但没有定论.我宁愿不直接连接到LDAP,我认为这是我组织中的一项政策违规行为,此外,我不确定是否有可能.如果可能的话,希望通过Outlook API进行此操作.
Any hints welcome. I have googled and read dozens of pages, but nothing conclusive. I'd rather not connect to the LDAP directly, I think that is a policy violation in my org and besides I am not sure it's possible. Would like to do this via the Outlook API if possible.
DEBUG=1
class MSOutlook:
def __init__(self):
self.outlookFound = 0
try:
self.oOutlookApp = \
win32com.client.gencache.EnsureDispatch("Outlook.Application")
self.outlookFound = 1
except:
print("MSOutlook: unable to load Outlook")
self.records = []
def loadContacts(self, keys=None):
if not self.outlookFound:
return
# this should use more try/except blocks or nested blocks
onMAPI = self.oOutlookApp.GetNamespace("MAPI")
ofContacts = \
onMAPI.GetDefaultFolder(win32com.client.constants.olFolderContacts)
if DEBUG:
print("number of contacts:", len(ofContacts.Items))
for oc in range(len(ofContacts.Items)):
contact = ofContacts.Items.Item(oc + 1)
if contact.Class == win32com.client.constants.olContact:
if keys is None:
# if we were't give a set of keys to use
# then build up a list of keys that we will be
# able to process
# I didn't include fields of type time, though
# those could probably be interpreted
keys = []
for key in contact._prop_map_get_:
if isinstance(getattr(contact, key), (int, str, unicode)):
keys.append(key)
if DEBUG:
keys.sort()
print("Fields\n======================================")
for key in keys:
print(key)
record = {}
for key in keys:
record[key] = getattr(contact, key)
if DEBUG:
print(oc, record['FullName'])
self.records.append(record)
随机链接:
MSDN - How to create a Global Address List programmatically using Visual Basic
Infinitec - How to get the Global Address List programatically
返回布尔值True -从Outlook/Exchange下载全局地址列表
Return Boolean True - Downloading the Global Address List from Outlook/Exchange
Grokbase -[python-win32]使用扩展的mapi获取全局通讯录
Grokbase - [python-win32] getting global addressbook with extended mapi
Stack Overflow - Searching Outlook Global Address
另一个尝试失败的列表
ActiveStater Code - RE: Email Address Lookup
Stack Overflow - Retrieving outlook Contacts via python
这是我上面的链接
Stack Overflow - Fetching Outlook Contacts with Python
根本不运行
适用于Windows的Python -自动化Microsoft Outlook
Python for Windows - Automating Microsoft Outlook
仅是默认通讯录.除了我要搜索,而不是全部列出.
Just default address book. Besides I want to search, not list all.
如果有人可以提出解决方案,我不介意它是C ++,VB,Perl,Python等.
If anyone can come up with a solution I don't mind if it's C++, VB, Perl, Python, etc.
推荐答案
当搜索字符串存在多个匹配项时,@ Prof.Falken解决方案中的方法并不总是有效.我发现了另一种解决方案,由于使用了displayname
的完全匹配项,因此更加健壮.
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
.
它的灵感来自如何从GAL(全局地址列表)获取addressEntry对象的完全匹配.
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 = 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)
这篇关于如何在Outlook(2010)全球地址列表中搜索名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!