简体   繁体   中英

Choose outlook account to send email / VBA

Hope everything is going well. This is the code I have to send emails from an Excel file, but I want to adapt it to be able to choose the outlook account from the emails are sent from ("abc@abc.com").

Sub SendEmail()
Dim i As Integer, Mail_Object, Email_Subject, o As Variant, lr As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
Set Mail_Object = CreateObject("Outlook.Application")
For i = 2 To lr
        With Mail_Object.CreateItem(o)
            .Subject = Range("B" & i).Value
            .To = Range("A" & i).Value
            .Body = Range("C" & i).Value
            '.CC = Range("G" & i).Value
            '.Send
            .display 'disable display and enable send to send automatically
    End With
Next i
        MsgBox "E-mail successfully sent", 64
        Application.DisplayAlerts = False
Set Mail_Object = Nothing
End Sub

I've seen different videos but I get a bit confused on the "Set" parts of the code. Can you help me with it please? Thanks

I think you're after the SentOnBehalfOfName property:

With Mail_Object.CreateItem(o)
    .Subject = Range("B" & i).Value
    .To = Range("A" & i).Value
    .Body = Range("C" & i).Value
    .SentOnBehalfOfName = "abc@abc.com"
    '.CC = Range("G" & i).Value
    '.Send
    .display 'disable display and enable send to send automatically
End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM