Send an e-mail based on the number of days until due

Even

Board Regular
Joined
Jan 1, 2013
Messages
81
Office Version
  1. 365
Platform
  1. Windows
Hi there, is it a way to generate an e-mail that includes the text I show beneath? When it is less den 90 days until renewal, I want to send out an e-mail to the person in charge of the customer about the renewal terms.

Send an automatic e-mail based on due date.xlsx
ABCDEFGHIJKLMNOPQR
15/17/2021
2General terms%Special termsCustomerDue dateTime until dueTransportD&OCyberCrimeArtName in chargeE-mail
3Transport4,50%5,00%Example 19/1/211074,50%3,50%5,00%
4D&O3,50%5,00%Example 211/30/211975,00%3,50%
5Cyber3,50%5,00%Example 37/1/21454,50%5,00%JohnJohn@test.com
6Crime3,50%5,00%Example 48/25/211005,00%5,00%
7Art3,50%5,00%Example 56/1/21154,50%3,50%5,00%JaneJane@test.com
8Example 67/30/21745,00%3,50%Jasonjason@test.com
9Example 77/1/21454,50%5,00%LydiaLydia@test.com
10Example 88/1/21765,00%5,00%EllenEllen@test.com
11Example 98/1/21763,50%
12Example 108/1/21763,50%
13
14
15Email SubjectRenewal
16Email BodyDear… it is soon time for renewal for X customer. The following standard terms of renewal is: Transport : 4,5% D&O: 3,5% Crime: 5% Please let me know if they will be renewing under these terms. Thank you! Sincerely X
17
All products
Cell Formulas
RangeFormula
A1A1=TODAY()
I3,I9,I7,I5I3=$B$3
L3,L10,L6:L7L3=$C$6
J3,J7J3=$B$4
J4,J10,J6,J8J4=$C$4
M4,M8M4=$B$7
K5,K9K5=$C$5
H3:H12H3=G3-$A$1
J11J11=B4
J12J12=B4
Cells with Data Validation
CellAllowCriteria
E3:E12List=$A$9:$A$11
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Here's a first attempt. Rather than use the text in the body cell G16, the code develops the same text in the large "body" portion in the middle. Also, the subject line is hard coded to be "Renewal", but if you want to make it based on G15, that is easily changed.

The body text is built as HTML-based code. The "<br>" text is a new line command. Other formatting can be used such as bolding certain words if desired.

VBA Code:
Option Explicit

Sub Send_email_under_90()
    Dim OutApp          As Object
    Dim OutMail         As Object
    Dim rCell           As Range
    Dim body            As String
    Dim colCustomer     As Integer 'Customer column
    Dim colTimeTillDue  As Integer 'Time until due column
    Dim colTransport    As Integer 'Transport column
    Dim colDandO        As Integer 'D&O column
    Dim colCyber        As Integer 'Cyber column
    Dim colCrime        As Integer 'Crime column
    Dim colArt          As Integer 'Art column
    Dim colName         As Integer 'Name in charge column
    Dim colEmail        As Integer 'Email column
    
    colCustomer = 6
    colTimeTillDue = 8
    colTransport = 9
    colDandO = 10
    colCyber = 11
    colCrime = 12
    colArt = 13
    colName = 15
    colEmail = 16
    
    For Each rCell In Range(Cells(3, colTimeTillDue), Cells(Rows.Count, colTimeTillDue).End(xlUp))
        If rCell.Value < 91 Then
            'Build body message
            body = "Dear " & Cells(rCell.Row, colName).Text & ",<br><br>"
            body = body & "It is soon time for renewal for " & Cells(rCell.Row, colCustomer).Text
            body = body & ". The following standard terms of renewal are:<br>"
            If Cells(rCell.Row, colTransport).Text <> "" Then
                body = body & "Transport: " & Cells(rCell.Row, colTransport).Text & "<br>"
            End If
            If Cells(rCell.Row, colDandO).Text <> "" Then
                body = body & "D&O: " & Cells(rCell.Row, colDandO).Text & "<br>"
            End If
            If Cells(rCell.Row, colCyber).Text <> "" Then
                body = body & "Cyber: " & Cells(rCell.Row, colCyber).Text & "<br>"
            End If
            If Cells(rCell.Row, colCrime).Text <> "" Then
                body = body & "Crime: " & Cells(rCell.Row, colCrime).Text & "<br>"
            End If
            If Cells(rCell.Row, colArt).Text <> "" Then
                body = body & "Art: " & Cells(rCell.Row, colArt).Text & "<br>"
            End If
            body = body & "<br>Please let me know if they will be renewing under these terms.<br><br>Thank you!<br><br>Sincerely X"
            
            'open outlook if OutApp hasn't been set to the Outlook App yet
            If OutApp Is Nothing Then
                Set OutApp = CreateObject("Outlook.Application")
            End If
            
            'Create email and display
            'The email can be automatically sent instead if ".Display" is replaced with ".Send"
            Set OutMail = OutApp.CreateItem(0)
            With OutMail
                .Subject = "Renewal"
                .To = Cells(rCell.Row, colEmail).Text
                .htmlbody = body
                .display
            End With
            Set OutMail = Nothing
        End If
    Next rCell
    Set OutApp = Nothing
End Sub
 
Upvote 0
Wow - that is amazing, shknbk2! Based on first look it seems to be working nicely. I need to be a little familiar with it and I get back to you :)
 
Upvote 0
That was a very fine code. It was easy to understand and to build on. But it opens the mail when the cell is blank in column H (time until due). Do you know how to open the mail only for non blank cells and of course when it is <90?
 
Upvote 0
All the cells will be filled so I don't need to ignore blank cells :)
 
Upvote 0
Glad to help. Let me know if any additional tweaks are needed.
 
Upvote 0
By the way: This is probably a new post?Is it possible to generate the same e-mail by clicking on the customer's name?
 
Upvote 0
You can create hyperlinks to macros: see here and here for examples.

However, if the customer names are going to be changing often enough to make creating these hyperlinks a pain, you can add a double-click event trigger for the worksheet that verifies if the cell that was double-clicked is within the range of the clients and if so, run the macro. The double-clicking event would avoid the need to create hyperlinks. It would kind of work similarly to the FollowHyperlink examples of the links above but using the BeforeDoubleClick event instead. A good example is part of this discussion here, see post #16 especially.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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