【问题标题】:Query Outlook Global Address List From Access VBA从 Access VBA 查询 Outlook 全局地址列表
【发布时间】:2020-07-13 19:26:46
【问题描述】:

我正在编写一些 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

【问题讨论】:

    标签: vba ms-access


    【解决方案1】:

    类似于我发布here 的答案,而不是检查MailItem 对象的To 属性(根据链接的文档,该属性仅包含显示名称),而是查询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
    

    【讨论】:

    • 所以我和你在一起,直到这里......WIth 块里面有什么?您说要查询 Recipients 集合的内容并检查每个 Recipient ......我该怎么做? Dim olMail As Outlook.MailItem With olMail With .Recipients End With End With
    • @HotTomales 我已经更新了我的答案,以将您推向正确的方向。您可能需要考虑在找到电子邮件地址后退出最内层的For Each 循环,以避免错误计算包含多次收件人的电子邮件。
    • 这很有意义!我试图用你另一篇文章中的 For Each 循环替换我原来的 For Each 循环。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多