'.SentOnBehalfOf and .SendUsingAccount on forward

I go into a shared inbox and forward an email as myself, to my boss, using HTML formatting.

I wrote VBA code that works on every step except changing the From line from the shared mailbox email address to my email address.

Let's call my personal email address "[email protected]" and the shared mailbox email address "[email protected]". If I manually click on the From field I can select my own email address and send it.

Public Sub ForwardIAF()
    Dim OutApp As Object
    Dim OutAccount As Outlook.Account
    Dim myinspector As Outlook.Inspector
    Dim myIAF As Outlook.MailItem
    Dim myForward As Outlook.MailItem
    
    Set myIAF = GetCurrentItem()
    Set myForward = myIAF.Forward
    Set OutApp = CreateObject("Outlook.Application")
    Set OutAccount = OutApp.Session.Accounts.Item(1)
    With myForward
        .SentOnBehalfOfName = "[email protected]"
        Debug.Print "myForward.SentOnBehalfOfName:" & "x " & myForward.SentOnBehalfOfName & " x"
        .Recipients.Add "[email protected]"
        .BodyFormat = olFormatHTML
        .Display
    End With
End Sub

When the forward opens, it shows my email address in the From line, but when I send, it reverts to the office email address. The debug print shows my email address is in the .SentOnBehalfOf field, so it looks like it's there until it sends.

Replacing .Display with .Send has the same result.



Solution 1:[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 ("[email protected]") 
    oMail.Recipients.ResolveAll 
    oMail.SendUsingAccount = oAccount 
    oMail.Send 
   End If 
  Next 
 End Sub 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Eugene Astafiev