choose account in outlook when send mail via excel vba
24,732
The solution is just to put Set in front of .SendUsingAccount
Set objMail = objOutlook.CreateItem(0)
signature = objMail.Body
With objMail
.To = cell.Value
.Subject = cell.Offset(0, 1).Value
.Body = cell.Offset(0, 2).Value & vbNewLine & signature
.Attachments.Add cell.Offset(0, 3).Value
.DeferredDeliveryTime = "15/03/2018 10:00:00 PM" 'need to comment here to run better
Set .SendUsingAccount = oAccount
.send
End With
And also Thanks to Maddy i commented after the deferredDeliveryTime and it went well through the oAccount
Related videos on Youtube
Comments
-
salom almost 4 years
I want to send mails from a specific account in outlook from VBA in excel and Im stuck with my code, i went over and over the forums but it still doesnt work
I show you my code if anyone could help me it would be very very nice
Sub SendMail() Dim objOutlook As Object Dim objMail As Object Dim ws As Worksheet Set objOutlook = CreateObject("Outlook.Application") Set ws = ActiveSheet Dim signature As String Dim LstRow As Long LstRow = ws.Cells(Rows.Count, 1).End(xlUp).Row Dim oAccount As Outlook.Account For Each oAccount In Outlook.Application.Session.Accounts If oAccount = "[email protected]" Then For Each cell In ws.Range("A4:A" & LstRow) Set objMail = objOutlook.CreateItem(0) signature = objMail.Body With objMail .To = cell.Value .Subject = cell.Offset(0, 1).Value .Body = cell.Offset(0, 2).Value & vbNewLine & signature .Attachments.Add cell.Offset(0, 3).Value .DeferredDeliveryTime = "15/03/2018 10:00:00 PM" .SendUsingAccount = oAccount .send End With Set objMail = Nothing Next cell Else End If Next Set ws = Nothing Set objOutlook = Nothing End Sub
-
Maddy about 6 yearsOn which line you are getting error? did you try
.SentOnBehalfOfName
? -
salom about 6 yearsThere is no errors, juste the nail is not sent
-
salom about 6 yearsYes I also tried senton behalf but it is the same thing
-
Maddy about 6 yearsWhere is
.Send
Or.Display
? -
salom about 6 yearssorry, it just after send using account
-
salom about 6 yearsthis line is weird also because it go through even if the account does not exist 'If oAccount = "[email protected]" Then'
-
Maddy about 6 yearscomment this line
.DeferredDeliveryTime = "15/03/2018 10:00:00 PM"
and then run. -
salom about 6 yearsNow it sending but not with the good account..
-
Maddy about 6 yearsgood account means?
-
salom about 6 yearsI have two account in outlook, the default one and "[email protected]" and i want the mail to be send from "[email protected]"
-
Maddy about 6 yearsnow try
.SentOnBehalfOfName
-
salom about 6 yearsStill sending with the default account..
-
salom about 6 yearsI FIND THE SOLUTION :)
-
salom about 6 yearsI need to add set in front of .sendUsingAccount
-
Maddy about 6 yearsare you using Outlook 2016?
-
salom about 6 yearsyes I use outlook 2016
-
Maddy about 6 years:) great !!!!!!
-
-
Timo over 2 years
Set .SendUsingAccount = oAccount
did not work, butSentOnBehalfOfName
worked for me forO365PP
for sending from another mailbox not my default one.