Auto changing the Email .SentOnBehalfOfName doesnt change the from section

gruntingmonkey

Active Member
Joined
Mar 6, 2008
Messages
444
Office Version
  1. 365
Platform
  1. Windows
I am trying to change the send from address when creating an email through VBA but can't seem to manage it. I have added " .SentOnBehalfOfName = "Test@Test.com"" but it doesnt change it in the email that it pops up.

VBA Code:
Sub SendFileTESTVERSION()

 On Error GoTo ErrHandler
    '''creates the email with details

    ' SET Outlook APPLICATION OBJECT.
    Dim objOutlook As Object
    Set objOutlook = CreateObject("Outlook.Application")
    
    ' CREATE EMAIL OBJECT.
    Dim objEmail As Object
    Set objEmail = objOutlook.CreateItem(olMailItem)
    
    Datestamp = Format(Date, "mmm yyyy")
    strbody = "Hello all," & _
              "<br><br>Please find attached the Dashboard for " & Datestamp & "." & _
              "<br><br>Kind regards,"
              
    EmailTo = Worksheets("Utilities").Cells(14, 4).Value
    Filept = "C:\Test\"
    FileNm = "Dashboard.xlsx"
    attfile1 = Filept + FileNm

'''Create email
    On Error Resume Next
    With objEmail
        .Display
        .SentOnBehalfOfName = "Test@Test.com"
        .To = EmailTo
        .Subject = "Dashboard for " & Datestamp & ""
        .HTMLBody = strbody & "<br>" & .HTMLBody
        .attachments.Add attfile1
        '.send
        .Display
    End With
    
     Set objEmail = Nothing:    Set objOutlook = Nothing
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing

Sheets("Utilities").Activate

Sheets("Utilities").Select
Cells(13, 3).Value = "Complete"

Exit Sub

ErrHandler:
MsgBox "There has been an error"
End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Works for me. Try without using the line On Error Resume Next while testing and see if your macro throws an error for that parameter.
 
Upvote 0
This is I think a bug with the VBA model. I have found that you can't display the email (.display) and then change the send on behalf of (.SentOnBehalfOfName). Simply re-order your code, and it should work.

VBA Code:
With objEmail
        [B].SentOnBehalfOfName = "Test@Test.com"[/B]
        .To = EmailTo
        .Subject = "Dashboard for " & Datestamp & ""
        .HTMLBody = strbody & "<br>" & .HTMLBody
        .attachments.Add attfile1
        '.send
        [B].Display[/B]
    End With
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top