从分发列表中的联系人获取Outlook联系人详细信息

时间:2016-10-15 18:18:12

标签: vba outlook distribution-list

由于无法使用Outlook中的通讯组列表创建套用信函(无法选择这些列表),我想阅读通讯组列表中联系人的联系信息。

根据API,应该可以获取ContactItem或使用属性访问器读取属性。两者都不起作用。 一种简单的方法是只读取联系人姓名(我可以从AddressEntry读取)并在Outlook通讯录中搜索。但我认为这很麻烦,可能容易出错。

请参阅下面的测试代码。 GetContact()和GetProperty()都不起作用。

是否有其他方法可以获取联系方式,如姓名,公司地址等?

Option Explicit

Sub test()
Dim myOlExp As Outlook.Explorer
Dim myOlSel As Outlook.Selection
Dim myOlDistList As Outlook.DistListItem
Dim nrListItems As Integer
Dim myRecipient As Outlook.Recipient
Dim myAddressEntry As Outlook.AddressEntry
Dim myContactItem As Outlook.ContactItem
Dim myPropertyAccessor As Outlook.propertyAccessor
Dim givenName As String

Set myOlExp = Application.ActiveExplorer
Set myOlSel = myOlExp.Selection
Set myOlDistList = myOlSel.Item(1)
For nrListItems = 1 To myOlDistList.MemberCount
    Set myRecipient = myOlDistList.GetMember(nrListItems)
    Set myAddressEntry = myRecipient.AddressEntry
    ' does not work
    Set myContactItem = myAddressEntry.GetContact

    Set myPropertyAccessor = myAddressEntry.propertyAccessor
    ' does also not work
    givenName = myPropertyAccessor.GetProperty("urn:schemas:contacts:givenName")
Next nrListItems
End Sub

1 个答案:

答案 0 :(得分:0)

是的,这是Outlook对象模型中的一个问题。我不知道使用Redemption

的解决方法
Dim myOlExp As Outlook.Explorer
Dim myOlSel As Outlook.Selection
Dim Session As Redemption.RDOSession
Dim myOlDistList As Redemption.RDODistListItem
Dim nrListItems As Integer
Dim myAddressEntry As Redemption.RDOAddressEntry

      set Session = CreateObject("Redemption.RDOSession")
      Session.MAPIOBJECT = Application.Session.MAPIOBJECT
      set myOlDistList = Session.GetMessageFromID(Application.ActiveExplorer.Selection(1).EntryID)
      For nrListItems = 1 To myOlDistList.MemberCount
        Set myAddressEntry = myOlDistList.GetMember(nrListItems)
        Set myContactItem = myAddressEntry.GetContact
        MsgBox myContactItem.FirstName
      next