Automatically Send Email When the date in time

AlaaEddin

New Member
Joined
May 2, 2018
Messages
25
Automatically send mail

our measure point is the "DATE" when it's in the place send the mail
our mail is depends on the cells in the excel I will provide a picture to explain what I mean

iK3bxK
iK3bxK
https://ibb.co/iK3bxK

I think from the image now you understand what I mean

Thanks.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
.
This macro has the SEND DATE in Col A. Leave as is or edit the code to suit.

Code:
Option Explicit
Sub eMail()
Dim lRow As Integer
Dim i As Integer
Dim toDate As Date
Dim toList As String
Dim eSubject As String
Dim eBody As String
Dim OutApp
Dim OutMail


With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .DisplayAlerts = False
End With


Sheets(1).Select
lRow = Cells(Rows.Count, 1).End(xlUp).Row


For i = 2 To lRow
toDate = Sheets("Sheet1").Cells(i, 1)


If toDate < Date Then: Exit Sub


  If Left(Cells(i, 3), 4) <> "Mail" And toDate = Date Then '
  
             Set OutApp = CreateObject("Outlook.Application")
             Set OutMail = OutApp.CreateItem(0)
        
                toList = Cells(i, 2)    'gets the recipient from col B
                eSubject = "Happy Birthday !"   'enter subject title here
                eBody = "Just taking a moment to wish you a very Happy Birthday !" & vbCrLf & "Hope it's a good day !"
                
                'enter email body here
                
                On Error Resume Next
                With OutMail
                .To = toList
                .CC = ""
                .BCC = ""
                .Subject = eSubject
                .Body = eBody
                .bodyformat = 1
                .Display   ' ********* Creates draft emails. Comment this out when you are ready
                '.Send     '********** UN-comment this when you  are ready to go live
                End With
         
            On Error GoTo 0
            Set OutMail = Nothing
            Set OutApp = Nothing
         Cells(i, 3) = "Mail Sent " & Date + Time 'Marks the row as "email sent in Column C"
             
    End If
Next i
ActiveWorkbook.Save


With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .DisplayAlerts = True
End With
MsgBox "All emails have been sent. ", vbInformation, "Email Notice "


End Sub
 
Upvote 0
.
This macro has the SEND DATE in Col A. Leave as is or edit the code to suit.

Code:
Option Explicit
Sub eMail()
Dim lRow As Integer
Dim i As Integer
Dim toDate As Date
Dim toList As String
Dim eSubject As String
Dim eBody As String
Dim OutApp
Dim OutMail


With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .DisplayAlerts = False
End With


Sheets(1).Select
lRow = Cells(Rows.Count, 1).End(xlUp).Row


For i = 2 To lRow
toDate = Sheets("Sheet1").Cells(i, 1)


If toDate < Date Then: Exit Sub


  If Left(Cells(i, 3), 4) <> "Mail" And toDate = Date Then '
  
             Set OutApp = CreateObject("Outlook.Application")
             Set OutMail = OutApp.CreateItem(0)
        
                toList = Cells(i, 2)    'gets the recipient from col B
                eSubject = "Happy Birthday !"   'enter subject title here
                eBody = "Just taking a moment to wish you a very Happy Birthday !" & vbCrLf & "Hope it's a good day !"
                
                'enter email body here
                
                On Error Resume Next
                With OutMail
                .To = toList
                .CC = ""
                .BCC = ""
                .Subject = eSubject
                .Body = eBody
                .bodyformat = 1
                .Display   ' ********* Creates draft emails. Comment this out when you are ready
                '.Send     '********** UN-comment this when you  are ready to go live
                End With
         
            On Error GoTo 0
            Set OutMail = Nothing
            Set OutApp = Nothing
         Cells(i, 3) = "Mail Sent " & Date + Time 'Marks the row as "email sent in Column C"
             
    End If
Next i
ActiveWorkbook.Save


With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .DisplayAlerts = True
End With
MsgBox "All emails have been sent. ", vbInformation, "Email Notice "


End Sub

I tried the code but it doesn't work for some reason I uploaded an image please take a look and see if there's something wrong I did

https://ibb.co/b3Pe2U

Thanks
 
Upvote 0
in the code that you post I need the body of the Email and the Subject of the Email with the email address and date all of those depends on the excel cells
because I don't want to make the process manually I want it to be automatically even the send button in outlook if there's a way to make it automatically send
without pressing that button it will be awesome.

Thanks a lot.
 
Upvote 0
.
Try this version :

Code:
Option Explicit
Sub eMail()
Dim lRow As Integer
Dim i As Integer
Dim toDate As Date
Dim toList As String
Dim eSubject As String
Dim eBody As String
Dim OutApp
Dim OutMail


With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .DisplayAlerts = False
End With


Sheets(1).Select
lRow = Cells(Rows.Count, 1).End(xlUp).Row


For i = 2 To lRow
toDate = Sheets("Sheet1").Cells(i, 1)


If toDate < Date Then: Exit Sub


  If Left(Cells(i, 5), 4) <> "Mail" And toDate = Date Then
  
             Set OutApp = CreateObject("Outlook.Application")
             Set OutMail = OutApp.CreateItem(0)
        
                toList = Cells(i, 2)    'gets the recipient from col B
                eSubject = Cells(i, 3)  'gets subject from col C
                eBody = Cells(i, 4)     'gets body of email from col D
                
                'enter email body here
                
                On Error Resume Next
                With OutMail
                .To = toList
                .CC = ""
                .BCC = ""
                .Subject = eSubject
                .Body = eBody
                .bodyformat = 1
                .Display   ' ********* Creates draft emails. Comment this out when you are ready
                '.Send     '********** UN-comment this when you  are ready to go live
                End With
         
            On Error GoTo 0
            Set OutMail = Nothing
            Set OutApp = Nothing
         Cells(i, 5) = "Mail Sent " & Date + Time 'Marks the row as "email sent in Column C"
             
    End If
Next i
ActiveWorkbook.Save


With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .DisplayAlerts = True
End With
MsgBox "All emails have been sent. ", vbInformation, "Email Notice "


End Sub


Sub sbInsertingRows()
    'Inserting a Row at at Row 2
    Range("A2").EntireRow.Insert
End Sub

Download workbook : https://www.amazon.com/clouddrive/share/yOlzHP24eiIMxnF2fB5y8FsIflivCFQPxJCH5CEqPsl
 
Upvote 0
I got this error

Message title: Microsoft Visual Basic

Message content: Run-time error "1004": Method 'sheets' of object'_Global' failed

what is that and why it happened ?

btw I tried to click send but nothing happened
 
Upvote 0
.
What line of code was highlighted when you received the error notification ?
 
Upvote 0
After closing the file and open it again the error didn't show again but the button send isn't doing anything at all
 
Upvote 0
.
Hmmm .... strange. Here the email is created and appears under the workbook. ???
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top