Sending an E-mail when a Due Date is Reached

by Allen Wyatt
(last updated March 26, 2016)

13

Domenic has a worksheet that shows due dates for projects in column E. He knows he can use conditional formatting to show when the due date is reached (when it is the same as today's date), but what he really needs is an e-mail to be sent when the due date is reached. He wonders if there is a way to do this in Excel.

Actually, there is a way to do this, provided you don't mind using a macro. In addition, you'll need to send the e-mail via Outlook, which VBA will communicate with just fine. (Unfortunately, VBA cannot be easily used to connect with other mail clients.)

Here, for example, is a macro that will run whenever your workbook opens. It automatically checks each row in a worksheet, specifically keying on two things: the due date in column E and a "flag value" in column F. (This flag value is set by the macro. If column F contains the letter "S," then the macro assumes an e-mail has previously been sent.)

Private Sub Workbook_Open()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim lLastRow As Long
    Dim lRow As Long
    Dim sSendTo As String
    Dim sSendCC As String
    Dim sSendBCC As String
    Dim sSubject As String
    Dim sTemp As String

    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon

    ' Change the following as needed
    sSendTo = "allen@xyz.com"
    sSendCC = ""
    sSendBCC = ""
    sSubject = "Due date reached"

    lLastRow = Cells(Rows.Count, 3).End(xlUp).Row
    For lRow = 2 To lLastRow
        If Cells(lRow, 6) <> "S" Then
            If Cells(lRow, 5) <= Date Then
                Set OutMail = OutApp.CreateItem(0)

                On Error Resume Next
                With OutMail
                    .To = sSendTo
                    If sSendCC > "" Then .CC = sSendCC
                    If sSendBCC > "" Then .BCC = sSendBCC
                    .Subject = sSubject

                    sTemp = "Hello!" & vbCrLf & vbCrLf
                    sTemp = sTemp & "The due date has been reached "
                    sTemp = sTemp & "for this project:" & vbCrLf & vbCrLf
                    ' Assumes project name is in column B
                    sTemp = sTemp & "    " & Cells(lRow,2)
                    sTemp = sTemp & "Please take the appropriate"
                    sTemp = sTemp & "action." & vbCrLf & vbCrLf
                    sTemp = sTemp & "Thank you!" & vbCrLf

                    .Body = sTemp
                    ' Change the following to .Send if you want to
                    ' send the message without reviewing first
                    .Display
                End With
                Set OutMail = Nothing

                Cells(lRow, 6) = "S"
                Cells(lRow, 7) = "E-mail sent on: " & Now()
            End If
        End If
    Next lRow
    Set OutApp = Nothing
End Sub

When the macro runs (again, when the workbook is first opened), it checks each row in the worksheet to see if there is an "S" in column F. If not, then it checks to see if the date in column E is equal to today's date. If it is, then the code puts together an e-mail message (which you can modify, as desired) to be sent. The e-mail is displayed, and you can click on the Send button after making any desired changes. At that point, the worksheet is updated by placing the "S" indicator in column F and the date the e-mail was sent into column G.

Note that the macro assumes that the name of the project is in column B. This information is used to put together the message that will be e-mailed.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (474) applies to Microsoft Excel 2007, 2010, 2013, and 2016.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Adding Hyperlinks

Adding a hyperlink to s text selection is easy to do in Word. All you need to do is make a couple of clicks and specify ...

Discover More

Returning an ANSI Value

Need to know the character value of the first character in a string? It's easy to do, without using a macro, by using the ...

Discover More

Locking Lines in a TOC

Want to "lock down" the lines in a TOC so that you cannot add new paragraph marks in the middle of one? You may not be ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Pasting a Hyperlink

Need a quick link within a document to some external data? You can paste information so that Excel treats it just like a ...

Discover More

Specifying a Browser in a Hyperlink

Excel allows you to easily add hyperlinks to a worksheet. Click on it, and the target of the link is opened in a browser ...

Discover More

ScreenTip for an Image

You can configure images in Excel so that if someone clicks on them, a macro is executed. You cannot, however, have a ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is five less than 7?

2018-05-16 13:22:35

nancy belcher

can an e-mail be sent if the workbook is NOT opened for alerts about approaching dates


2018-03-02 16:50:28

John

@JAB
' Change the following to .Send if you want to
' send the message without reviewing first
.Display


2018-03-01 13:28:33

JAB

How can this script be modified to cause the email to be sent automatically?


2018-02-05 20:09:45

tomuko

why can't I change that s "flag Value" to 100%????


2018-02-04 23:26:07

tomuko

Hi, what if I want to send email reminder every ten days before due date will be reached, if reach, and every next ten days for three times? and stop if they already send the project
I also wondering if I can send the email to a lot of people according to which group the project is held, so if a project is from group a, I will send the notification to everyone in group a.
please help me, thank you.


2017-11-25 06:20:35

Josh

Hi,

This article helps a lots, what if I would like to attach one image in the same email ?


2017-07-27 07:05:54

Gettin Bedder

This macro works for me except it does not put the projects name into the email


2017-03-17 03:32:25

S.Sebastian

Amazing stuff worked well for me with some tweaks into this Code. Good work Alan :-)


2016-03-31 07:38:51

Steve

I could not get this to work until I c hanged the following;

lLastRow = Cells(Rows.Count, 3).End(xlUp).Row

to;
lLastRow = Cells(Rows.Count, 2).End(xlUp).Row


I have never been able to get this to run when the worksheet is opened. I can only get it to run when I manually run it.


2016-03-29 00:20:52

Satyajit Pradhan

Dear Sir - all is well in this tip of sending mails via excel but no SCREENSHOT EXAMPLES given whichw e can relate and get exactly what to do.


2016-03-27 07:25:42

Barry

It's also quite easy to interface to a Gmail account using the CDO library.


2016-03-26 10:26:52

Bengt Eriksson

Dear Allan,
In response to your tip about sending e-mail through Excel, and particularly about your line "VBA cannot be easily used to connect with other mail clients.", I would just like to comment that it connects very well also to Lotus Notes, in more or less the same way as it connects to Outlook. On extra bonus with Lotus Notes is that you can choose - programatically - into which folder the mail that you are sending should go. If the folder does not exist, it is created.

Thanks for all your valuable tips.

Bengt


2016-03-26 05:30:51

Cruiser

This is beautiful, but I would like to be able to send an sms on the due date, via thesim card in my modem. Thanks if you can point me to it.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.