我试图在我通过Outlook 2016发送的每封电子邮件上设置。sentonbehalfofname。也就是说,每当我点击"新邮件"、"回复"、"全部回复"或"转发"时。
我试过了:
Public WithEvents myItem As Outlook.MailItem
Private Sub Application_ItemLoad(ByVal Item As Object)
If (TypeOf Item Is MailItem) Then
Set myItem = Item
End If
End Sub
Private Sub FromField()
With myItem
.SentOnBehalfOfName = "example@aol.com"
.Display
End With
End Sub
Private Sub myItem_Open(Cancel As Boolean)
FromField
End Sub
SentOnBehalfOfName属性仅在Exchange配置文件/帐户的情况下才有意义。此外,您需要具有代表其他人发送所需的权限。参见问题与SentOnBehalfOfName进行类似的讨论。
如果您在配置文件中配置了多个帐户,则可以使用SendUsingAccount属性,该属性允许使用Account对象来表示要发送MailItem的帐户。
Sub SendUsingAccount()
Dim oAccount As Outlook.account
For Each oAccount In Application.Session.Accounts
If oAccount.AccountType = olPop3 Then
Dim oMail As Outlook.MailItem
Set oMail = Application.CreateItem(olMailItem)
oMail.Subject = "Sent using POP3 Account"
oMail.Recipients.Add ("someone@example.com")
oMail.Recipients.ResolveAll
oMail.SendUsingAccount = oAccount
oMail.Send
End If
Next
End Sub
In ThisOutlookSession
Private WithEvents sentInsp As Inspectors
Private WithEvents sentMailItem As mailItem
Private Sub Application_Startup()
Set sentInsp = Application.Inspectors
End Sub
Private Sub sentInsp_NewInspector(ByVal Inspector As Inspector)
If Inspector.currentItem.Class = olMail Then
Set sentMailItem = Inspector.currentItem
sentMailItem.SentOnBehalfOfName = "someone@someplace.com"
End If
End Sub
我发现我的事件代码必须通过每隔一段时间运行启动重置。ItemSend可能更可靠。
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim copiedItem As MailItem
If Item.Class = olMail Then
Set copiedItem = Item.Copy
copiedItem.SentOnBehalfOfName = "someone@someplace.com"
'copiedItem.Display
copiedItem.Send
Item.Delete
Cancel = True
End If
Set copiedItem = Nothing
End Sub
当我运行这段代码时,它不会再次调用ItemSend。
使用应用程序。
这就是原来问题的答案。
将代码放入'ThisOutlookSession'
Option Explicit
Public WithEvents myItem As Outlook.MailItem
Public EventsDisable As Boolean
Private Sub Application_ItemLoad(ByVal Item As Object)
'https://stackoverflow.com/questions/21727768/rule-that-runs-macro-when-an-email-is-opened
If EventsDisable = True Then Exit Sub
If Item.Class = olMail Then
Set myItem = Item
End If
End Sub
Private Sub myItem_Open(Cancel As Boolean)
On Error Resume Next
Dim copiedItem As MailItem
Set copiedItem = myItem.Copy
copiedItem.SentOnBehalfOfName = "someone@someplace.com"
copiedItem.Display
Cancel = True 'This cancels 'myItem' from opening for the user because we only want 'copiedItem' to open.
End Sub
我花了大约三个星期才得到这个答案。这要归功于尼顿的回答,它帮助我找到了这个问题。
使用此方法允许您在电子邮件显示给用户之前调整。sentonbehalfofname属性。与Application_ItemSend方法相反,Application_ItemSend方法在用户点击"发送"后改变。sentonbehalfofname属性。
注意,在电子邮件显示给用户之前,需要调整。sentonbehalfofname属性。
你不能在Application_ItemLoad方法中调整太多,这是我需要使用'myItem'来复制'Item',执行你的逻辑,然后将'myItem'复制到'copiedItem',然后设置copiedItem。SentOnBehalfOfName属性,然后最后使用。display向用户显示'copiedItem'