如何在Outlook(2010)全局地址列表中搜索名称?

2024-10-03 15:27:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一份名单,有些是完整的,有些是删节的。我想在Outlook地址列表中搜索这些名称的匹配项。在

最接近我的是这个Python代码which came from ActiveState Code,但它不搜索全局地址,只搜索我的(local?)列表中有3个地址,这显然是不对的。应该有成千上万的记录。在

欢迎任何提示。我在谷歌上搜索并阅读了几十页,但没有确凿的结论。我不想直接连接到LDAP,我认为这在我的组织中违反了策略,而且我也不确定这是否可能。如果可能的话,希望通过outlookapi来实现这一点。在

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)

随机链接:

如果有人能想出一个解决方案,我不介意,如果是C++,VB,perl,python等等。

Tags: oftokeyindebugselfforif
3条回答

上面的代码处理默认联系人文件夹中的联系人。如果您想检查某个给定的名字是否在Outlook中(无论是作为联系人还是GAL),只需调用Application.Session.CreateRecipient,然后再调用Recipient.Resolve。如果调用返回true,则可以读取Recipient.Address和其他各种属性。在

方法@Falken教授当搜索字符串存在多个匹配项时,的解决方案并不总是有效。我找到了另一个解决方案,它使用displayname的精确匹配,因此更加健壮。在

它的灵感来自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 = 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)

问题解决了!

多亏了Dmitry'sanswers,我可以生成这个最小的Python代码,它演示了我想要实现的目标:

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 = 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)

相关问题 更多 >