Home > Net >  Select an not default sender in Excel VBA code
Select an not default sender in Excel VBA code

Time:08-28

Here is the code:

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

I try with ".from" in the code but it doesn't work.

How can I set the automatic mail in excel sent by my secondary account in outlook?

Thank you in advance.

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