Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Send multiple email to one email address with specific date

0

Can anyone help me I try to make multiple email send in different date to one email address with excel. I have create cells (A2:A5) containts different date but right now I'm having a problem automation error

Sub Send_Deferred_Mail_From_Excel()
Dim i As Integer, OutlookMail, lr As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
Dim xRg As Range

Set xRg = Range("A2 :A5")
Set OutlookMail = CreateObject("Outlook.Application")

For i = 2 To lr
With OutlookMail.CreateItem(0)
.To = "email@gmail.com"
.CC = ""
.Subject = ""
.Body = ""

'Send email on specific day & time
.DeferredDeliveryTime = Range("A2") + Range("A3") + Range("A4") + Range("A5") & "8:00:00 aM"
'.Send
.Display 'disable display and enable send to send automatically

End With
Next i
Application.DisplayAlerts = False
Set OutlookMail = Nothing
End Sub

Answer
Discuss

Discussion

Hi Kimurios

Did you see my Answer (below) from Monday? If it works for you, please mark the Answer as Selected (in accordance with the rule of the Forum). if not, please comment in the Discussion under the Answer (don't reply as an Answer- those should be reserved for solutions to the Questions)
John_Ru (rep: 6142) Feb 8, '23 at 3:17 am
Hey there, can you help me in my code in excel for this send multiple email send in different date to one email address. I've added new cells for subject (B2:B5). Currently right now i wanna to make changes in my code .subject=(B2:B5). Should I declare my cells first Set Range ("B2:B5"). Can it be done because I get automation error by doing that. 
kimurios (rep: 2) Feb 14, '23 at 7:04 pm
I already answered you- please see the Discussion below my Answer
John_Ru (rep: 6142) Feb 14, '23 at 7:15 pm
Okay, I get my answer already, Thank you
kimurios (rep: 2) Feb 14, '23 at 7:56 pm
Add to Discussion

Answers

0
Selected Answer

Hi Kimurios and welcome to the Forum

In the attached file, I've made the changes in bold so it uses your variables lr and xRrg better. I've corrected the code I believe caused the Automation Error:

Sub Send_Deferred_Mail_From_Excel()

Dim i As Integer, OutlookMail As Object, lr As Long
Dim xRg As Range, Cl As Range

' get last row in A
lr = Cells(Rows.Count, "A").End(xlUp).Row
' create range from row 2 to last row
Set xRg = Range("A2:A" & lr)
Set OutlookMail = CreateObject("Outlook.Application")

'For i = 2 To lr
For Each Cl In xRg

    With OutlookMail.CreateItem(0)
        .To = "email@gmail.com"
        .CC = ""
        .Subject = ""
        .Body = ""

        'Send email on specific day & time
        '.DeferredDeliveryTime = Range("A2") + Range("A3") + Range("A4") + Range("A5") & "8:00:00 AM"
        .DeferredDeliveryTime = Cl.Value & " 8:00:00 AM"
        '.Send
        .Display 'disable display and enable send to send automatically (and set OutlookMail to Nothing)

    End With
Next Cl

'Application.DisplayAlerts = False

' Temporarily disabled for testing
'Set OutlookMail = Nothing

End Sub

Run the macro and it will produce (and only display) several emails on however many dates are in column A (make sure they're in the future and don't leave a gap!), mine are 7 consecutive dates. I disabled your line after the new comment:

' Temporarily disabled for testing
'Set OutlookMail = Nothing

so that Outlook doesn't disappear before you examine the emails which it creates. In each displayed email, look under Options/ Delay Deleivery to confirm the dates are set correctly.

The problem lay in your line:

.DeferredDeliveryTime = Range("A2") + Range("A3") + Range("A4") + Range("A5") & "8:00:00 aM"

since your trying to add 5 ranges (inside a For i= loop without using i, which doesn't work that way) whereas Outlook needs a date before the time.

I commented that line out and my replacement line (in bold above) is:

        .DeferredDeliveryTime = Cl.Value & " 8:00:00 AM"

inside a For Each... loop which looks at each cell (the variable Cl) and concatenates that with your 8 AM time string.

Tip- If you want to add the code into your own file, don't make the chnages manually- use Select All in the header at the start of my code and copy the selected text etc.

Hope this works well for you (with 4 or more dates)- if so, please mark this Answer as Selected (that's how the Forum works).

Discuss

Discussion

I still facing an error which in automation error in 
.DeferredDeliveryTime = Cl.Value & " 8:00:00 AM"

kimurios (rep: 2) Feb 8, '23 at 9:59 pm
Are you sure that there is a valid date in the cells? It should be a date without a time portion - if you change the cell format to number then enter the cell, it should read something like  44966 (a positive number wjth no decimal portion).

If that doesn't work, please edit your original question to attach your Excel file using the Add Files button. 
John_Ru (rep: 6142) Feb 9, '23 at 2:25 am
I get it now why it's error because I set my cell A2:A5 as date & time which lead to automation error.  I've changed my cells to date and it's works. Thank you so much for the help
kimurios (rep: 2) Feb 9, '23 at 7:07 pm
Great, glad it's working now. Thanks for selecting my Answer, Kimurios. 
John_Ru (rep: 6142) Feb 10, '23 at 3:22 am
Hey there, can you help me I've added new cells (B2:B5) as Subject. How do I need to declare the cells should I declare it as Set xRg = Range (B2:B5)
kimurios (rep: 2) Feb 14, '23 at 3:59 am
Hi Kimurios. No, that wouldn't work but since your new Subject cells are one to the right of the send date cells, you can change the line in bold below:
For Each Cl In xRg
 
    With OutlookMail.CreateItem(0)
        .To = "email@gmail.com"
        .CC = ""
        .Subject = ""

to:
 
        .Subject = Cl.Offset(0,1).Value

i.e. the make the Subject the value of the cell offset by 0 rows and 1 columns (to the right) of the date cell, Cl. Hope this helps- kindly confirm that this change works for you.

For the future, please note that we don't favour follow-on questions (once the original question has been answered) but prefer a new question to be raised. That prevents users saying "What about this?", "What about if I want to do this?" etc. and covering several issues under one question title.
John_Ru (rep: 6142) Feb 14, '23 at 6:51 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login