首页 > 解决方案 > 如何使用 SenderEmailAddress 检查收到的电子邮件?

问题描述

我有一个循环来检查来自 Joe Doe 的带有特定主题的传入电子邮件。该循环有效并保存了来自 Joe Doe 的附件。

If (Msg.SenderName = "Doe, Joe") And _
        (Msg.Subject = "Test: Smartsheet") And _
        (Msg.Attachments.Count >= 1) Then

我想使用 SenderEmailAddress 而不是 SenderName。

I tested below codes:

    If (Msg.SenderEmailAddress = "test@noreplay.com") And _
            (Msg.Subject = "Test: Smartsheet") And _
            (Msg.Attachments.Count >= 1) Then

    If (SenderEmailAddress = "test@noreplay.com") And _
            (Msg.Subject = "Test: Smartsheet") And _
            (Msg.Attachments.Count >= 1) Then

标签: vbaoutlook

解决方案


第二段新代码肯定行不通,因为SenderEmailAddress它是Msg.

Msg.SenderEmailAddress = "test@noreplay.com"返回True必须完全匹配。“Test@noreplay.com”或“test@NoReplay.com”或任何其他此类变体将不会返回True

建议一:

恢复原始代码但添加额外语句

If (Msg.SenderName = "Doe, Joe") And _
   (Msg.Subject = "Test: Smartsheet") And _
   (Msg.Attachments.Count >= 1) Then
  Debug.Print Msg.SenderEmailAddress

运行宏后,即时窗口将包含 Joe Doe 电子邮件的发件人电子邮件地址列表,以便您检查它们是否符合您的预期。

建议二:

使比较不区分大小写,因此:

If (LCase(Msg.SenderEmailAddress) = "test@noreplay.com") And _
        (Msg.Subject = "Test: Smartsheet") And _
        (Msg.Attachments.Count >= 1) Then

推荐阅读