【问题标题】:Copying Global Address List contacts including "External Contacts"复制全局地址列表联系人,包括“外部联系人”
【发布时间】:2015-10-07 00:43:34
【问题描述】:

我有一个 VBA 代码可以从 Outlook 2013 获取整个全球地址列表,并将值 Name 和 E-mail Address 放在 Excel 工作表中。

问题是它只从我的 SMTP 返回电子邮件/用户(我猜)。

在这张图片中,我们可以看到来自 SMTP 的用户被黑色覆盖,而外部用户被红色覆盖。我的代码:

Sub tgr()

    Dim appOL As Object
    Dim oGAL As Object
    Dim oContact As Object
    Dim oUser As Object
    Dim arrUsers(1 To 75000, 1 To 2) As String
    Dim UserIndex As Long
    Dim i As Long

    Set appOL = CreateObject("Outlook.Application")

    Set oGAL = appOL.GetNameSpace("MAPI").AddressLists("Global Address List").AddressEntries

    For i = 1 To oGAL.Count
        Set oContact = oGAL.Item(i)
        If oContact.AddressEntryUserType = 0 Then
            Set oUser = oContact.GetExchangeUser
            If Len(oUser.lastname) > 0 Then
                UserIndex = UserIndex + 1
                arrUsers(UserIndex, 1) = oUser.Name
                arrUsers(UserIndex, 2) = oUser.PrimarySMTPAddress
            End If
        End If
    Next i

    appOL.Quit

    If UserIndex > 0 Then
        Range("A2").Resize(UserIndex, UBound(arrUsers, 2)).Value = arrUsers
    End If

    Set appOL = Nothing
    Set oGAL = Nothing
    Set oContact = Nothing
    Set oUser = Nothing
    Erase arrUsers

End Sub

那么,我是不是做错了什么?

【问题讨论】:

    标签: vba excel email outlook gal


    【解决方案1】:

    根据this documentation,oContact.AddressEntryUserType 值应包括外部用户的olExchangeRemoteUserAddressEntry (5)。

    您的代码中的内容只是列出 Exchange 用户,因此它还会跳过启用邮件的 PublicFolders、分发列表等。


    编辑
    找到了一种更好的方法来提取姓名和电子邮件地址(如果有):
    参考:Obtain the E-mail Address of a Recipient
    Option Explicit
    
    Sub tgr()
        Const PR_SMTP_ADDRESS = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
        Dim appOL As Object
        Dim oGAL As Object
        Dim arrUsers() As String
        Dim UserIndex As Long
        Dim i As Long
        Dim sEmail As String
    
        Set appOL = GetObject(, "Outlook.Application")
        If appOL Is Nothing Then Set appOL = CreateObject("Outlook.Application")
    
        Set oGAL = appOL.GetNameSpace("MAPI").AddressLists("Global Address List").AddressEntries
        Debug.Print oGAL.Parent.Name & " has " & oGAL.Count & " entries"
        ReDim arrUsers(1 To oGAL.Count, 1 To 2)
        On Error Resume Next
        For i = 1 To oGAL.Count
            With oGAL.Item(i)
                Application.StatusBar = "Processing GAL entry #" & i & " (" & .Name & ")"
                sEmail = "" ' Not all entries has email address
                sEmail = .PropertyAccessor.GetProperty(PR_SMTP_ADDRESS)
                If Len(sEmail) = 0 Then Debug.Print "No Email address configured for " & .Name & " (#" & i & ")"
                UserIndex = UserIndex + 1
                arrUsers(UserIndex, 1) = .Name
                arrUsers(UserIndex, 2) = sEmail
            End With
        Next
        On Error GoTo 0
        Application.StatusBar = False
        appOL.Quit
    
        If UserIndex > 0 Then
            Range("A2").Resize(UserIndex, UBound(arrUsers, 2)).Value = arrUsers
        End If
    
        Set appOL = Nothing
        Set oGAL = Nothing
        Erase arrUsers
    
    End Sub
    

    【讨论】:

    • 是的。你说得对。使用“oContact.AddressEntryUserType = 0 或 oContact.AddressEntryUserType = 5”,它返回了相同的先前结果 + 另一个 7k 联系人。包括我域外的电子邮件。但是我仍然在 Outlook 中的 GAL 中看到一些地址,并且它们没有在使用该代码的工作表中返回。
    猜你喜欢
    • 2015-12-21
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    • 2011-05-16
    • 2018-02-22
    相关资源
    最近更新 更多