'How check incoming emails using SenderEmailAddress?

I have a loop to check for incoming emails from Joe Doe with specific subject. That loop works and saves attachments from Joe Doe.

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

I want to use SenderEmailAddress instead of SenderName.

I tested below codes:

    If (Msg.SenderEmailAddress = "[email protected]") And _
            (Msg.Subject = "Test: Smartsheet") And _
            (Msg.Attachments.Count >= 1) Then

And

    If (SenderEmailAddress = "[email protected]") And _
            (Msg.Subject = "Test: Smartsheet") And _
            (Msg.Attachments.Count >= 1) Then


Solution 1:[1]

The second piece of new code will definitely not work because SenderEmailAddress is a property of Msg.

For Msg.SenderEmailAddress = "[email protected]" to return True there must be an exact match. "[email protected]" or "[email protected]" or any other such variation would not return True.

Suggestion 1:

Restore the original code but add an additional statement

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

After you have run the macro, the Immediate Window will contain a list of the sender email addresses of Joe Doe's emails so you can check they are as you expect.

Suggestion 2:

Make the comparison case-insensitive so:

If (LCase(Msg.SenderEmailAddress) = "[email protected]") And _
        (Msg.Subject = "Test: Smartsheet") And _
        (Msg.Attachments.Count >= 1) Then

Solution 2:[2]

Keep in mind that MailItem.SenderEmailAddress and MailItem.SenderName are read-only properties in the Outlook Object Model. Also note that on the MAPI level, there are half a dozen or so sender related properties, most important being the sender entry id (that is what is used for replies). Ditto for the SentRepresentingXYZ properties. I don't think MailItem.PorpertyAccessor.SetProperty will let you set all of these properties.

If using Redemption (I am its author) is an option, it allows to set all sender related properties.

  set Session = CreateObject("Redemption.RDOSession")
  Session.MAPIOBJECT = Application.Session.MAPIOBJECT
  set rdoMsg = Session.GetMessageFormID(Msg.EntryID)
  vSenderEntryId = Session.AddressBook.CreateOneOffEntryID("Joe The Sender", "SMTP", "[email protected]", false, true)
  set vSender = Session.AddressBook.GetAddressEntryFromID(vSenderEntryId)
  rdoMsg.Sender = vSender
  rdoMsg.SentOnBehalfOf = vSender
  Msg.Save

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tony Dallimore
Solution 2