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

时间:2015-08-25 14:15:04

标签: python vba perl outlook outlook-2010

我有一个名单,其中一些是完整的,一些是截断的。我想在Outlook地址列表中搜索这些名称的匹配项。

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

欢迎任何提示。我用Google搜索并阅读了几十页,但没有结论。我宁愿不直接连接到LDAP,我认为这是我的组织中的政策违规,除此之外我不确定是否可能。如果可能,希望通过Outlook API执行此操作。

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等。

3 个答案:

答案 0 :(得分:5)

问题解决了!

感谢Dmitry's answers我可以制作这个最小的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)

答案 1 :(得分:4)

上面的代码处理默认“联系人”文件夹中的联系人。如果要检查给定名称是否在Outlook中(作为联系人或GAL),只需调用Application.Session.CreateRecipient,然后调用Recipient.Resolve。如果调用返回true,您可以阅读Recipient.Address和其他各种属性。

答案 2 :(得分:1)

当搜索字符串存在多个匹配项时,@ Prof.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)