首页 > 解决方案 > 如何从 Outlook 联系人返回部门信息

问题描述

我正在尝试使用 python 在 GAL 上查找 Outlook 联系人并返回他们的部门信息(与系统中的 Outlook 帐户相关联)。我可以访问联系人列表并根据姓名进行搜索,但找不到返回部门的方法。是否可以通过 python 访问该信息?

import win32com.client
o = win32com.client.gencache.EnsureDispatch("Outlook.Application")
ns = o.GetNamespace("MAPI")

adrLi = ns.AddressLists.Item("Global Address List")
contacts = adrLi.AddressEntries

nameAliasDict = {}

for i in contacts:
    Name = i.Name
    x = ?   #Not sure what to make x equal to in order to make it department information
    if Name == 'FirstN LastN':
        print(x)

我知道部门信息已附加到 Outlook 联系人,因为每当我尝试向某人发送电子邮件并搜索他们的姓名时,它都会显示在表格中。

标签: pythonoutlook

解决方案


不要遍历地址簿容器中的所有条目 - 一些 GAL 容器可以包含 100,000 多个条目。使用Namespace.CreateRecipient/ Recipient.Resolve。这与在 Outlook 的“收件人”编辑框中键入名称并按 Ctrl+K 解决它的效果相同。获得已解析的收件人对象后,调用Recipient.AddressEntry.GetExchangeUser().Department.准备处理错误并AddressEntry.GetExchangeUser()返回 null。


推荐阅读