根据收件人设置发件人电子邮件地址

时间:2017-05-17 08:52:47

标签: vba outlook outlook-addin outlook-vba

如何以电子邮件方式设置,我回复,发件人地址为收件人地址?

enter image description here

VBA有办法吗?我不知道从哪里开始,所以我道歉,因为我无法显示任何代码。

2 个答案:

答案 0 :(得分:1)

归功于@Al Bundy纠正此问题。此解决方案基于this post

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

答案 1 :(得分:0)

Outlook不允许您将邮件发件人设置为任意电子邮件地址 - 您必须具有明确的权限(如果是Exchange)才能设置MailItem.SentOnBehalfOfName属性。如果是POp3 / SMTP帐户,请设置MailItem.Account属性。