The provided VBA code automates the sending of email reminders...
The provided VBA code automates the sending of email reminders through Microsoft Outlook for tasks in an Excel worksheet. Here's a breakdown of how it works:
Overall Purpose:
This script identifies tasks from a worksheet that meet certain criteria (e.g., tasks not yet completed, with due dates within a specific range) and sends email reminders to task owners and agents on a weekly basis (only on Mondays). It also keeps track of when reminders were last sent to avoid frequent notifications.
Key Components:
-
Configuration Section:
- Defines key constants, such as the sheet name (
Tasks Individual
), header row, and relevant column numbers for data like due dates, status, owners, and agents. - Maps task statuses (e.g.,
Not Started
,Completed
). - References the email directory sheet (
EmailDirectory
), which pairs people's names with their email addresses. - Allows toggling between sending and previewing emails via the
PREVIEW_EMAIL
constant.
- Defines key constants, such as the sheet name (
-
Main Routine:
SendOwnerAgentReminders_WeeklyMonday()
:- Runs Only on Mondays:
- The macro exits immediately if the current day is not Monday.
- Validates and Prepares Worksheet:
- Checks if the "Tasks Individual" worksheet and its columns are properly set up.
- Ensures the presence of a "Last Notified" column to log when emails were sent last.
- Email Logic per Task:
- Iterates through all rows (tasks) in the worksheet, starting from the second row (after the header).
- Filters out tasks that:
- Are marked as "Completed."
- Don't have valid due dates.
- Are outside the due date window (between the "Due Date" and "Regulatory Due Date").
- Fall within the 7-day period after a previous notification.
- Constructs email addresses for the task owner and reporting agent, resolving names into email addresses if necessary (via
EmailDirectory
).
- Composes and Sends Emails:
- Builds a plain-text email body (describing the task contents) and sends emails to the relevant recipients using Microsoft Outlook.
- Updates the "Last Notified" column with the current date for sent notifications.
- Tracks and reports the number of emails sent.
- Error Handling:
- Skips any rows or operations with invalid/missing data and gracefully handles Outlook object creation errors.
- Runs Only on Mondays:
-
Helper Functions:
FindHeaderColumn()
: Finds the column index for a given column header.IsEmail()
: Checks if a string is a valid email format (e.g., contains@
).NormalizeEmails()
: Removes extra spaces or converts commas to semicolons for proper email format.LookupEmail()
: Searches theEmailDirectory
worksheet for a recipient's name to retrieve their email.ResolveRecipient()
: Returns an actual email address, either directly (if provided) or by resolving a name usingLookupEmail()
.BuildPlainTextBody()
: Constructs the plain text for the email body, detailing the task's relevant details (e.g., report name, due dates, status, owner, agent, etc.).
Key Logic in Filtering Tasks:
For each row in the task sheet:
- Skip tasks where:
- The "Status" is empty or "Completed."
- Due dates are invalid or the current date (
today
) is outside the range between the "Due Date" and "Regulatory Due Date." - The task was previously notified within the last 7 days.
- Resolve the email addresses of the owner and reporting agent from the
EmailDirectory
, ensuring valid email format. - Send a reminder email for tasks that pass all these checks.
Outputs:
- Email Sending:
- Sends individualized reminders with subject lines like:
Weekly Reminder: [Report Name] — Action Needed
- Email body summarizes task details, like due dates, owner, agent, and includes instructions to update the status of the task.
- Sends individualized reminders with subject lines like:
- Tracking Notifications:
- Updates the "Last Notified" column with the current date for each task that was emailed.
- Summary Message:
- After execution, displays a message box indicating how many reminders were sent (e.g., "5 weekly reminder(s) sent.").
Use Case:
This code is useful for organizations or project managers who need to automate task follow-ups. It ensures weekly notifications are sent to the right people (owners/agents) at the right time, reducing manual oversight.