1
votes

I have some code that partially populates an email message as a reply. But I can not get the sender unless they are on (an / our)? exchange server.

Public Sub CreateMessage()

Dim EmailFrom As String
Dim NewMessage As Outlook.MailItem
Dim OldMessage As Outlook.MailItem


Set OldMessage = Application.ActiveInspector.CurrentItem
Set NewMessage = Application.CreateItem(olMailItem)
EmailFrom = OldMessage.Sender.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x39FE001E")
NewMessage.Body = Body(EmailFrom)
NewMessage.HTMLBody = HTMLBody(EmailFrom)
NewMessage.Recipients.Add (EmailFrom)
NewMessage.Display
Set NewMessage = Nothing

End Sub

The message I receive is "The property "http://schemas.microsoft.com/mapi/proptag/0x39FE001E" is unknown or cannot be found." and it only appears to work with internal messages.

Anyone know of a way in Outlook VBA to get the sender of a mail message that works for all of them?


Fixed based on Dimitry's comments:

Public Sub CreateMessage()

Dim EmailFrom As String
Dim NewMessage As Outlook.MailItem
Dim OldMessage As Outlook.MailItem


Set OldMessage = Application.ActiveInspector.CurrentItem
Set NewMessage = Application.CreateItem(olMailItem)
Select Case OldMessage.SenderEmailType
    Case "EX"
        EmailFrom = OldMessage.Sender.GetExchangeUser.PrimarySmtpAddress
    Case Else
        EmailFrom = OldMessage.SenderEmailAddress
End Select
NewMessage.Body = Body(EmailFrom)
NewMessage.HTMLBody = HTMLBody(EmailFrom)
NewMessage.Recipients.Add (EmailFrom)
NewMessage.Display
Set NewMessage = Nothing

End Sub
1

1 Answers

3
votes

You are requesting the PR_SMTP_ADDRESS property, which is Exchange specific. Check if SenderEmailType is "EX" and only then read the PR_SMTP_ADDRESS property. Otherwise just read the MailItem.SenderEmailAddress property.

Take a look at the message with OutlookSpy (click IMessage) to see the available properties.