如何在 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 Code,但它不搜索全局地址,只搜索我的(本地?)列表,其中有 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.
欢迎任何提示.我用谷歌搜索并阅读了几十页,但没有定论.我宁愿不直接连接到 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
======================================")
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
List 的另一个失败尝试
Another failed attempt at List
ActiveStater Code - RE: Email Address Lookup
堆栈溢出 - 通过 python 检索 Outlook 联系人
Stack Overflow - Retrieving outlook Contacts via python
这是我得到上面链接的地方
This is where I got the link above
堆栈溢出 - 使用 Python 获取 Outlook 联系人
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屋!