Pro*_*ica 3 python perl outlook vba outlook-2010
我有一个名单,其中一些是完整的,一些是截断的.我想在Outlook地址列表中搜索这些名称的匹配项.
我最接近的是这个来自ActiveState Code的 Python代码,但是它不搜索全局地址,只搜索我的(本地?)列表,其中有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)
Run Code Online (Sandbox Code Playgroud)
随机链接:
另一次尝试失败的List
Stack Overflow - 通过python检索outlook联系人
这是我上面的链接
根本不运行
适用于Windows的Python - 自动化Microsoft Outlook
只是默认地址簿.此外,我想搜索,而不是列出所有.
如果有人能提出解决方案,我不介意它是C++,VB,Perl,Python等.
上面的代码处理默认“联系人”文件夹中的联系人。如果要检查给定名称是否在Outlook中(作为联系人还是在GAL中),只需先致电,Application.Session.CreateRecipient然后致电Recipient.Resolve。如果调用返回true,您可以阅读Recipient.Address和其他各种属性。
问题解决了!
感谢Dmitry的 回答,我可以生成这个最小的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)
Run Code Online (Sandbox Code Playgroud)