从Access VBA查询Outlook全局地址列表

时间:2019-07-07 22:11:03

标签: vba access-vba ms-access-2016

我正在编写一些Access VBA代码,以获取特定电子邮件地址已通过电子邮件发送多少次的计数。我遇到的问题是,第一次发送电子邮件时,电子邮件将我们的Exchange服务器的地址设为

email1@domain.com

但是一旦该人回复了该电子邮件,则所有后续消息均显示为

'lastname, firstname'

我使用下面的VBA代码搜索email1@domain.com示例,但是如何使用access vba从全局地址列表中获取名称?

Function Test()

Dim searchEmail As String: searchEmail = "'abc123@abc123.com'"
Dim olApp As Outlook.Application
Dim olNs As NameSpace
Dim Fldr As MAPIFolder
Dim olReply As Outlook.MailItem
Dim msg As Object
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderSentMail)

For Each msg In Fldr.Items
    If TypeName(msg) = "MailItem" Then
        If msg.To = searchEmail Then
            'now we start counting
        End If
    End If
Next msg

End Function

1 个答案:

答案 0 :(得分:1)

类似于我发布的here的答案,而不是检查To对象的MailItem属性(根据链接的文档,该属性仅包含显示名称),而是查询Recipients集合的内容,并针对每个Recipient对象,针对您的searchEmail变量测试Address属性所拥有的值。

Address属性将始终包含收件人的电子邮件地址,而不是显示名称。

也就是说,代替:

For Each msg In Fldr.Items
    If TypeName(msg) = "MailItem" Then
        If msg.To = searchEmail Then
            'now we start counting
        End If
    End If
Next msg

您可能会使用类似的内容:

For Each msg In Fldr.Items
    If TypeName(msg) = "MailItem" Then
        For Each rcp In msg.Recipients
            If rcp.Address = searchEmail Then
                'now we start counting
            End If
        Next rcp
    End If
Next msg