我可以使用帮助修改 Outlook VBA 宏。每当我从多个电子邮件帐户中的任何一个回复电子邮件时,脚本都会将发件人地址更改为指定的地址(即代表 group@domain.com 的 user@domain.com)。我喜欢这种行为,但需要帮助进行更改,以便此脚本仅在我从电子邮件地址 @domain.com 发送时运行。本质上,我希望宏有一个 if 语句,指定是否从 @domain.com 电子邮件帐户发送然后运行宏,否则如果从另一个电子邮件帐户发送,即 user@gmail.com 不运行宏。
'================================================================================
'Description: Outlook macro to automatically set a different
' From address.
'
'Comment: You can set the email address at the bottom of the code.
' Uncomment the myOlExp_InlineResponse sub to also make it
' work with the Reading Pane reply feature of Outlook 2013/2016/2019/365.
'
' author : Robert Sparnaaij
' version: 1.1
' website: https://www.howto-outlook.com/howto/setfromaddress.htm
'================================================================================
Dim WithEvents objInspectors As Outlook.Inspectors
Dim WithEvents objMailItem As Outlook.MailItem
Dim WithEvents myOlExp As Outlook.Explorer
Private Sub Application_Startup()
Initialize_handler
End Sub
Public Sub Initialize_handler()
Set objInspectors = Application.Inspectors
Set myOlExp = Application.ActiveExplorer
End Sub
Private Sub objInspectors_NewInspector(ByVal Inspector As Inspector)
If Inspector.CurrentItem.Class = olMail Then
Set objMailItem = Inspector.CurrentItem
If objMailItem.Sent = False Then
Call SetFromAddress(objMailItem)
End If
End If
End Sub
'Uncomment the next 3 lines to enable Outlook 2013/2016/2019/365 Reading Pane Reply
'Private Sub myOlExp_InlineResponse(ByVal objItem As Object)
' Call SetFromAddress(objItem)
'End Sub
Public Sub SetFromAddress(oMail As Outlook.MailItem)
' Set your preferred default From address below.
' Exchange permissions determine if it is actually stamped
' as "Sent On Behalf Of" or "Sent As".
' The address is not properly updated for the InlineResponse
' feature in Outlook 2013/2016/365. This is only a visual bug.
oMail.SentOnBehalfOfName = "delegate@domain.com"
End Sub
答案 0 :(得分:0)
如果您想处理外发电子邮件,您需要订阅 Application 类的 ItemSend 事件,每当发送 Microsoft Outlook 项目时都会触发该事件,或者由用户通过检查器(在检查器关闭之前) ,但在用户单击“发送”按钮之后)或在程序中使用 Outlook 项目(例如 MailItem)的 Send 方法时。
Public WithEvents myOlApp As Outlook.Application
Public Sub Initialize_handler()
Set myOlApp = Outlook.Application
End Sub
Private Sub myOlApp_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim prompt As String
prompt = "Are you sure you want to send " & Item.Subject & "?"
If MsgBox(prompt, vbYesNo + vbQuestion, "Sample") = vbNo Then
Cancel = True
End If
End Sub
在事件处理程序中,您可以查看 MailItem.SendUsingAccount 属性,该属性允许设置一个 Account
对象,该对象代表要发送 MailItem
的帐户。 SendUsingAccount
属性可用于指定在调用 Send 方法时应用于发送 MailItem
的帐户。
根据在邮件项目上设置的帐户,您可能希望设置 MailItem.SentOnBehalfOfName 属性,该属性返回一个字符串,指示邮件消息的预期发件人的显示名称。您可能需要取消默认操作并以编程方式重新提交项目。
答案 1 :(得分:0)
将文件夹树导航到电子邮件地址文件夹。
这应该是 objMailItem.Parent.Parent
。
Private Sub objInspectors_NewInspector(ByVal Inspector As Inspector)
If Inspector.currentItem.Class = olMail Then
Set objMailItem = Inspector.currentItem
If objMailItem.Sent = False Then
Debug.Print objMailItem.Parent.Parent
If InStr(LCase(objMailItem.Parent.Parent), LCase("@domain.com")) Then
Call SetFromAddress(objMailItem)
End If
End If
End If
End Sub