在每封发送的电子邮件上设置.SentOnBehalfofName

时间:2015-10-25 15:17:58

标签: vba outlook-vba outlook-2016

我正在尝试在通过Outlook 2016发送的每封电子邮件上设置.SentOnBehalfOfName。也就是说,每当我点击New Mail,Reply,Reply All或Forward。

我试过了:

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

4 个答案:

答案 0 :(得分:1)

SentOnBehalfOfName属性仅在Exchange个人资料/帐户的情况下才有意义。此外,您需要具有代表其他人发送所需的权限。有关类似的讨论,请参阅Issue with SentOnBehalfOfName

如果您在配置文件中配置了多个帐户,则可以使用SendUsingAccount属性,该属性允许使用表示将在其下发送MailItem的帐户的Account对象。

 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 

答案 1 :(得分:0)

改为使用Application.ItemSend事件。

答案 2 :(得分:0)

在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。

答案 3 :(得分:0)

这是原始问题的答案。

将代码放在“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

这个答案花了我大约三个星期才得到。归功于 niton 的回答,它帮助我找到了这个答案。

使用此方法可让您在向用户显示电子邮件之前调整 .SentOnBehalfOfName 属性。与 Application_ItemSend 方法相反,该方法在用户单击“发送”后更改 .SentOnBehalfOfName 属性。

请注意,在向用户显示电子邮件之前,需要调整 .SentOnBehalfOfName 属性。

您不能在 Application_ItemLoad 方法中进行太多调整,这是我需要使用“myItem”复制“Item”,执行您的逻辑,然后将“myItem”复制到“copiedItem”,然后设置 CopyItem.SentOnBehalfOfName 属性,以及然后最后使用 .Display 向用户显示 'copiedItem'。

相关问题