Home > database >  Select a non-default sender using Excel VBA
Select a non-default sender using Excel VBA

Time:08-30

How can I send from a secondary Outlook account using Excel VBA?

With OutMail
    .to = Text(1)
    .CC = Text(2)
    .BCC = ""
    .Subject =text(3)
    .HTMLBody = Text(10)
    .Display '.send
End With

I tried ".from".

CodePudding user response:

The below is from a pretty all-purpose sub I use to send emails from VBA.
It takes three parameters:

  • OutApp is an Outlook.Application object
  • SendFromAddress is a string variable containing the address to send from
  • OutMail is the current mailitem object, which you already have as a variable looking at your code.
'Sender Address
If Len(SendFromAddress) > 0 Then
    'if directly signed into the account:
    For a = 1 To OutApp.Session.accounts.Count
        If LCase(OutApp.Session.accounts.Item(a)) Like LCase(SendFromAddress) Then
            Outmail.sendusingaccount = OutApp.Session.accounts.Item(a)
            SendFromAddress = ""
            Exit For
        End If
    Next
    'If not directly signed in (shared mailbox):
    If Len(SendFromAddress) > 0 Then Outmail.SentOnBehalfOfName = SendFromAddress
End If
  • Related