Email Notification

TaskMaster

Board Regular
Joined
Oct 15, 2020
Messages
55
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi,

I'm wondering if anyone can help me. I have thrown together a simple spreadsheet that would list a series of tasks assigned to a person. When the task is overdue I'm wanting it to send an email reminder to that person. Unfortunately I get an error when using the following "There must be at least one name or contact group in the To, Cc, or Bcc box". Once I get to a stage where I can send emails, I would like if possible to have a dynamic body which lists the task(s) that are overdue for each person and when the due date is or by how many days its overdue.

Thanks in advance.

SQL:
Sub SendEmailReminder()

Dim OutlookApp As Object
Dim OutlookMailItem As Object
Dim iCounter As Integer
Dim MailDest As String

Set OutlookApp = CreateObject("Outlook.application")
Set OutlookMailItem = Outlook.CreateItem(0)

With OutlookMailItem
MailDest = ""
For iCounter = 1 To WorksheetFunction.CountA(Columns(12))
If MailDest = "" And Cells(iCounter, 12).Offset(0, -5) = "OVERDUE" Then
MailDest = Cells(iCounter, 12).Value
ElseIf MailDest <> "" And Cells(iCounter, 12).Offset(0, -5) = "OVERDUE" Then
MailDest = MailDest & ";" & Cells(iCounter, 12).Value

End If

Next iCounter

.To = MailDest
.Subject = "Overdue Tasks"
.Body = "Please review the checklist and complete overdue tasks"
.Send

End With

SetOutlookItem = Nothing
SetOutlookApp = Nothing

End Sub
 

Attachments

  • Error.PNG
    Error.PNG
    6.1 KB · Views: 14

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Have you checked to make sure that the value of MailDest is a valid string of email addresses?
Rather than automatically sending with the email with .Send, it's usually best to replace that with .Display while you're trying iron out any bugs in the code. If you displayed the the resulting email rather than send it, it might show you where the problem is when you manually go to send it.
 
Upvote 0
Hi, Dan apologies I thought I had attached the spreadsheet previously.

The display was a good idea, I can see that no email addresses are populated in the email. Is there something obvious that I have got wrong?

TASKPRIORITYASSIGNED TOPROGRESSSTARTDUEEST HOURSSTATUSDAYS TO COMPLETEDONECOMMENTSEMAIL
TASK 1 Title
DashboardLowTest1100%08/10/202108/10/20211COMPLETED0YTest1@outlook.com
Task 1.2HighTest110%02/10/202107/10/20215OVERDUE-89Test1@outlook.com
Task 1.3HighTest1100%03/10/202112/10/20215COMPLETED0YTest1@outlook.com
Task 1.4MediumTest2100%04/10/202113/10/20219COMPLETED0YTest2@outlook.com
TASK 2 TitleMediumTest220%04/10/202119/10/20218OVERDUE-77Test2@outlook.com
 

Attachments

  • Capture1.PNG
    Capture1.PNG
    28.2 KB · Views: 10
Upvote 0
I tried out your code, and stepped through it on a line-by-line basis. This approach to debugging is useful (and will save you headaches), because you can see how the values of variables change and you can start to see where things go wrong. You can do this by pressing F8 to execute the code (rather than F5). There are plenty of guides online on how to do go about doing this, and I'm certain it will save you plenty of time in future.

Anyway, what I learnt from stepping your code was this:
  1. Cells(iCounter, 12).Offset(0, -5) - this references the wrong column. It's looking at the value of Estimated Hours, and not the status. This is easily solved by changing the -5 to -4.
  2. After fixing that, your code still didn't fully work. It only returned Test1@outlook.com, and not both that and Test2@outlook.com, which I suspect was the intended result? The reason for that was the use of WorksheetFunction.CountA(Columns(12)) to get the upper limit of the loop combined with the use of 1 as the lower boundary. COUNTA tells you how many cells in a range have some content in them. In your spreadsheet, you've designated the range to be the entire column. That includes the header row and the empty row 2. So it's return 6 cells as having content, so the loop is looking at rows 1 to 6 (i.e,. the header row and the empty row 2 + the next 4). Which means the final row with Test2 falls out of the loop entirely. You can fix this by clarifying the lower and upper bounds of the loop. You may want to consider using a function to find the last row of a data set, rather than using COUNTA, on the off chance your column 12 has other blank cells in it.
I hope that makes sense, but let me know if I've only made things more confusing!

After making those corrections, the MailDest returned: Test1@outlook.com;Test2@outlook.com
 
Upvote 0
Solution
Hi Dan,

Appreciate you looking into this for me and identifying my errors. From this I came up with the following which brings back Test1@outlook.com;Test2@outlook.com as expected. Thank you for pointing me in the right direction!

VBA Code:
Sub EmailReminder()

Application.DisplayAlerts = False

Dim apOutlook As Object

Dim aEmail As Object

Dim RangeAddresses As Range, RangeCell As Range, Assigned_To As String

Set apOutlook = CreateObject("Outlook.Application")

Set aEmail = apOutlook.CreateItem(0)
 

'find e-mails address's to send reminder

Set RangeAddresses = ThisWorkbook.Worksheets("Task Tracker").Range("L:L")

For Each RangeCell In RangeAddresses.Cells

If Cells(RangeCell.Row, "H").Value = "OVERDUE" Then

Assignee = Assignee & ";" & RangeCell.Value

End If

Next
 
Application.DisplayAlerts = True

'write the email

With aEmail

'Set Subject

.Subject = "Overdue Tasks"

'Set Body for mail

.Body = "Please review the checklist and complete overdue tasks"

'Set Recipient and send

.To = Assignee

.Display

End With

MsgBox ("Reminder Email Sent")

End Sub
 
Upvote 0
That's great - I'm glad you got it working. Does it send the emails ok now?
Thank you for the update!
 
Upvote 0
Hi Dan,

Yes, I forgot to change it back from .Display to .Send but works perfectly!
 
Upvote 0
Excellent! That's really good to hear. Have a great weekend!
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,199
Members
449,072
Latest member
DW Draft

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