Outlook - 分发列表成员详细信息

时间:2017-04-14 20:20:28

标签: vba outlook exchange-server outlook-vba

我正在尝试在分发列表中获取用户的详细信息(包含约200人)。

当我创建新电子邮件时,将此DL添加为唯一收件人并运行下面的宏,它将返回~15个第一个结果,然后" Outlook正在尝试从Microsoft Exchange服务器检索数据"托盘消息出现,一段时间后我得到"操作失败"错误。

如果我继续执行代码,则会返回下一个~15个值,并再次出现此问题。这样的接缝有一些Exchange反垃圾邮件限制。

Sub GetDetails(olMail As MailItem)

    Dim i As Integer, j As Integer
    For i = 1 To olMail.Recipients.Count ' count = 1
        If olMail.Recipients.Item(i).AddressEntry.GetExchangeUser Is Nothing Then
            For j = 1 To olMail.Recipients.Item(i).AddressEntry.Members.Count ' count ~= 200
                Debug.Print olMail.Recipients.Item(i).AddressEntry.Members.Item(j).GetExchangeUser.FirstName
            Next j
        End If
    Next i
End Sub

但是,如果我展开分发列表(使用' +'图标)并运行稍微修改过的代码,则会返回所有用户的结果而不会出现任何问题(仅需几秒钟)。

Sub GetDetails(olMail As MailItem)

    Dim i As Integer
    For i = 1 To olMail.Recipients.Count ' count ~= 200
        If Not olMail.Recipients.Item(i).AddressEntry.GetExchangeUser Is Nothing Then
            Debug.Print olMail.Recipients.Item(i).AddressEntry.GetExchangeUser.FirstName
        End If
    Next i
End Sub

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您需要立即在代码中发布Outlook COM对象。如果您的加载项尝试枚举存储在Microsoft Exchange Server上的集合中超过256个Outlook项目,这一点尤为重要。如果您未及时发布这些对象,则可以达到Exchange对任何时候打开的最大项目数的限制。完成后,只需将变量设置为Nothing即可释放对象的引用。

答案 1 :(得分:0)

根据Eugene的反馈更新(工作)代码:

Sub GetDetails(olMail As MailItem)
    Dim oRecipients As Recipients
    Dim oRecipient As Recipient
    Dim oMembers As AddressEntries
    Dim oMember As AddressEntry
    Dim i As Integer, j As Integer, dRecCnt As Integer, dMemCnt As Integer

    Set oRecipients = olMail.Recipients

    dRecCnt = oRecipients.Count
    For i = 1 To dRecCnt
        Set oRecipient = oRecipients.Item(i)

        If oRecipient.AddressEntry.GetExchangeUser Is Nothing Then
            Set oMembers = oRecipient.AddressEntry.Members

            dMemCnt = oMembers.Count
            For j = 1 To dMemCnt
                Set oMember = oMembers.Item(j)

                Debug.Print c & ": " & oMember.GetExchangeUser.FirstName

                Set oMember = Nothing
            Next j

            Set oMembers = Nothing
        End If

        Set oRecipient = Nothing
    Next i

    Set oRecipients = Nothing
End Sub