在每封发送的电子邮件中设置“代表名称”。

3
我正在尝试在通过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
4个回答

1

SentOnBehalfOfName属性仅适用于Exchange配置文件/账户。此外,您需要具备发送代表其他人的必要权限。请参阅SentOnBehalfOfName问题以了解类似的讨论。

如果您在配置文件中有多个帐户,则可以使用SendUsingAccount属性,该属性允许选择一个表示应该发送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 

好的,我猜为了抽象一下,每次我点击“新邮件”,“回复”,“回复全部”和“转发”时,VBA代码会是什么样子来自动执行某个操作呢?比如每次都添加一个收件人?我知道这与我最初提问的有些不同,但是SendOnBehalfOfName对我所需的功能有效,上次我问这个问题时就卡在了那一点上。感谢您的帮助! - ExPerseides
在 Inspector 类的 NewInspectorActivate 事件中,您可以检查它是否显示为新项目,并设置适当的属性。 - Eugene Astafiev
请将上述建议的完整代码打出来。 - Mark

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

非常感谢niton。我使用了你上面提供的"ItemSend"建议,用于我所需的内容,并且它起作用了。通过以上方法,我实际上能够在某种方式上进行项目加载,我会在有时间时发布我是如何做到的。 - Mark

0
请使用 Application.ItemSend 事件。

0

这是原问题的答案。

将代码放置在 'ThisOutlookSession' 内部。

Option Explicit

Public WithEvents myItem As Outlook.MailItem
Public EventsDisable As Boolean

Private Sub Application_ItemLoad(ByVal Item As Object)

'https://dev59.com/vXzaa4cB1Zd3GeqPPmhn

    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”,然后设置 copiedItem.SentOnBehalfOfName 属性,最后使用 .Display 显示“copiedItem”给用户。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接