Setting Outlook From address RRS feed

  • Question

  • We are using the code below in Access 2016 and it works great.  However, one of our users has 2 email addresses in their Outlook setup.  We would like to have the "From" on the created email to have the 2nd address (accounting@mydomain.com) always come up in the "From" address.  Currently it comes up with the users default email address of Lori@mydomain.com and would like to set it in code.  Any help is appreciated.

                Dim olApp As New Outlook.Application
                Dim mItem As Outlook.MailItem 
                Set olApp = CreateObject("Outlook.Application")
                Set mItem = olApp.CreateItem(olMailItem)
                Dim iFlag As Integer, i As Integer
                iFlag = 1
                For i = 1 To olApp.Session.Accounts.Count
                    If olApp.Session.Accounts.Item(i) = "accounting@mydomain.com" Then
                        iFlag = i
                    End If
                Next i
                ' Add the To/Subject/Body to the message and display or send the message
                With mItem
                    .Recipients.Add strTo
                    .Subject = strSubject
                    .HTMLBody = "Attached is your invoice"
                    .SendUsingAccount = olApp.Session.Accounts.Item(iFlag)
                    .Attachments.Add stOutFile
                    If bolSend = True Then
                        .Display    ' To show the email message to the user
                    End If
                End With
                ' Release all object variables
                Set mItem = Nothing
                Set olApp = Nothing

    Thursday, February 28, 2019 6:16 PM

All replies

  • Check out the SentOnBehalfOfName property.

    -Tom. Microsoft Access MVP

    • Marked as answer by David Chase89 Friday, March 1, 2019 3:03 PM
    • Unmarked as answer by David Chase89 Monday, March 4, 2019 4:34 PM
    Friday, March 1, 2019 2:58 AM
  • This did work but the email was placed into the first mail account Sent Items rather than the second email address Sent Items.  Is there a way to control it so that mail goes into the accounting email address?  It is the 2nd email address on this users Outlook accounts.
    Monday, March 4, 2019 4:36 PM