0

How can I set programmatically in an email, to which I reply, the sender address to the recipient address?

enter image description here

Is there a way in VBA? I don't know where to start so I apologize because I cannot show any code.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Peter VARGA
  • 4,780
  • 3
  • 39
  • 75
  • 1
    Can you be a bit more specific? What are you trying to achieve? I think you can write some code that will create a reply to the mail and match the mail's recipient with one of your accounts, is that what you want? You can easily find the code to create the mail's reply and get the mail's recipient. – R3uK May 17 '17 at 09:11
  • 1
    Try the `.SentOnBehalfOfName` property. – Kostas K. May 17 '17 at 09:23
  • @Al Bundy Apologies, I misread your question earlier on. See my answer below. – Kostas K. May 17 '17 at 20:10

2 Answers2

1

Credit to @Al Bundy for correcting this. This solution is based on this post.

In ThisOutlookSession:

Option Explicit

Private WithEvents objMail As MailItem
Private assignmentHandled As Boolean

'Set MailItem
Private Sub Application_ItemLoad(ByVal Item As Object)
    If Item.Class = olMail And Not assignmentHandled Then
        Set objMail = Item
    End If
End Sub

'Handle reply/replayAll from triggering ItemLoad again
Private Sub objMail_Open(Cancel As Boolean)
    assignmentHandled = True
End Sub

'Reply
Private Sub objMail_Reply(ByVal Response As Object, Cancel As Boolean)
    Call SetSentOnBehalfOfName(Response)
End Sub

'Reply all
Private Sub objMail_ReplyAll(ByVal Response As Object, Cancel As Boolean)
    Call SetSentOnBehalfOfName(Response)
End Sub

'MailItem closed
Private Sub objMail_Close(Cancel As Boolean)
    assignmentHandled = False
End Sub

' Avoid repeating code
Private Sub SetSentOnBehalfOfName(ByRef Response As Object)
    Response.SentOnBehalfOfName = objMail.To
    assignmentHandled = False
End Sub
Community
  • 1
  • 1
Kostas K.
  • 8,293
  • 2
  • 22
  • 28
0

Outlook will not let you set message sender to an arbitrary email address - you must have an explicit permission (in case of Exchange) to set the MailItem.SentOnBehalfOfName property. In case of a POp3/SMTP account, set the MailItem.Account property.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78