3

I am trying to set the .SentOnBehalfOfName on every email I send through Outlook 2016. That is, whenever I hit New Mail, Reply, Reply All, or Forward.

I tried this:

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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
ExPerseides
  • 31
  • 1
  • 1
  • 3

4 Answers4

1

The SentOnBehalfOfName property makes sense only in case of Exchange profiles/accounts. Moreover, you need to have the required permissions to send on behalf of another person. See Issue with SentOnBehalfOfName for a similar discussion.

In case if you have multiple accounts configured in the profile you can use the SendUsingAccount property which allows to an Account object that represents the account under which the MailItem is to be sent.

 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 
Community
  • 1
  • 1
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Okay, I guess to abstract it a bit, what would the VBA code look like to automatically do something every time I click New Mail, Reply, Reply All, and Forward? Say adding a recipient every time? I know this is a bit different from what I originally asked, but the SendOnBehalfOfName works for what I need, and the last time I asked a question about this it got bogged down on that point. Thanks for the help! – ExPerseides Oct 25 '15 at 22:30
  • In the `NewInspector` or `Activate` event of the Inspector class you may check whether it is shown for a new item and set an appropriate property. – Eugene Astafiev Oct 26 '15 at 07:34
  • Please type out the entire code for the above suggestion. – Mark Apr 13 '21 at 18:18
1

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

I have found my event code has to be reset by running startup at intervals. ItemSend may be more reliable.

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

When I run this code it does not call ItemSend again.

niton
  • 8,771
  • 21
  • 32
  • 52
  • Thank you so much niton. I used your "ItemSend" suggestion above into what I needed and it worked. With the above, I was actually able to do it upon item load in a certain way which I will post how I did when I have the time. – Mark Apr 13 '21 at 21:17
0

Use the Application.ItemSend event instead.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
0

This is the answer to the original question.

Place the code inside of '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

This answer took me about three weeks to get. Credit to niton's answer which helped me get to this one.

Using this method allows you to adjust the .SentOnBehalfOfName property before the email is displayed to the user. As opposed to the Application_ItemSend method which changes the .SentOnBehalfOfName property after the user clicks 'Send'.

Note, the .SentOnBehalfOfName property needs to be adjusted before the email is displayed to the user.

You cannot adjust much in the Application_ItemLoad method which is my you need to use 'myItem' to copy 'Item', perform your logic, then copy 'myItem' to 'copiedItem', then set the copiedItem.SentOnBehalfOfName property, and then finally use .Display to display 'copiedItem' to the user.

Mark
  • 178
  • 1
  • 2
  • 16