3

我在 Excel 中有一个名字和姓氏的列表,我想利用该列表使用 Visual Basic 在 Outlook 中查找电子邮件地址。

我正在使用以下 VB 代码:

    Private Sub GetAddresses()
    Dim o, AddressList, AddressEntry
    Dim c As Range, r As Range, AddressName As String
    Set o = CreateObject("Outlook.Application")
    Set AddressList = o.Session.AddressLists("Global Address List")
    Set r = Range("a1:a3")
    For Each c In r
        AddressName = Trim(c.Value) & ", " & Trim(c.Offset(0, 1).Value)
        For Each AddressEntry In AddressList.AddressEntries
            If AddressEntry.Name = AddressName Then
                c.Offset(0, 2).Value = AddressEntry.Address
                Exit For
            End If
        Next AddressEntry
    Next c
    End Sub

在实际检索电子邮件地址之前,该代码似乎运行良好。在它匹配一个名称后,它返回以下而不是地址。有谁知道我做错了什么。

/O=Compnay/OU=Company/cn=Recipients/cn=shs

提前感谢您的帮助。

4

2 回答 2

6

我假设这些是域用户。您想从 exchangeUser 对象中获取 SMTP 地址。我已经更新了您的代码以显示这一点。

Private Sub GetAddresses()
    Dim o, AddressList, AddressEntry
    Dim c As Range, r As Range, AddressName As String
    'added variable for exchange user object
    Dim exchangeUser As Outlook.exchangeUser

    Set o = CreateObject("Outlook.Application")
    Set AddressList = o.Session.AddressLists("Global Address List")
    Set r = Range("a1:a3")
    For Each c In r
        AddressName = Trim(c.Value) ' & ", " & Trim(c.Offset(0, 1).Value)
        For Each AddressEntry In AddressList.AddressEntries
            If AddressEntry.Name = AddressName Then
            'set the exchange user object
            Set exchangeUser = AddressEntry.GetExchangeUser
            'get the smtp addresss
            c.Offset(0, 2).Value = exchangeUser.PrimarySmtpAddress
            'release
            Set exchangeUser = Nothing
                Exit For
            End If
        Next AddressEntry
    Next c
End Sub
于 2012-12-11T17:37:47.057 回答
0

哎哟! 为什么要遍历地址列表中可能包含数十个条目的所有项目?使用 Aplication.Sesssion.CreateRecipient,然后调用 Recipient.Resolve。如果成功,您可以从 Recipient.AddressEntry 检索 AddressEntry 对象。
如果您需要确保仅针对 GAL 解析名称(顺便说一下,您不应该对 GAL 名称进行硬编码,它会因语言环境而异),您可以使用Redemption及其 AddreessList.ResolveName 方法 - 您需要做的就是是调用RDOSession.AddressBook.GAL.ResolveName

于 2013-02-24T00:00:19.413 回答