Garden Utopia Productions
New Member
- Joined
- Mar 25, 2019
- Messages
- 14
I'm starting to learn VBA. I've got a list of tasks that are due on various days. They are divided and organized by different departments. What I need to do is scan through the whole range of dates and then automatically send an email with the tasks that are due today.
I found some VBA code that essentially does this, except it creates an email for each task that's due today. I want to be able to generate one email with the list of tasks in that email. I know I need to move the call to open Outlook outside the For loop, but I don't know how to do it correctly.
Here's the code below:
Thanks in advance!
I found some VBA code that essentially does this, except it creates an email for each task that's due today. I want to be able to generate one email with the list of tasks in that email. I know I need to move the call to open Outlook outside the For loop, but I don't know how to do it correctly.
Here's the code below:
Code:
Public Sub CheckAndSendMail()
Dim xRgDate As Range
Dim xRgDepartment As Range
Dim xRgTask As Range
Dim xOutApp As Object
Dim xMailItem As Object
Dim xLastRow As Long
Dim i As Long
Dim vbCrLf As String
Dim xMailBody As String
Dim xRgDateVal As String
Dim xRgSendVal As String
Dim xMailSubject As String
Dim xIsDateANumber As Boolean
On Error Resume Next
Set xRgDate = Application.InputBox("Please select the due date column:", "Task Checker", , , , , , 8)
If xRgDate Is Nothing Then Exit Sub
Set xRgDepartment = Application.InputBox("Select the Department column:", "Task Checker", , , , , , 8)
If xRgDepartment Is Nothing Then Exit Sub
Set xRgTask = Application.InputBox("Select the Tasks Number column:", "Task Checker", , , , , , 8)
If xRgTask Is Nothing Then Exit Sub
xLastRow = xRgDate.Rows.Count
Set xRgDate = xRgDate(1)
Set xRgDepartment = xRgDepartment (1)
Set xRgTask = xRgTask(1)
Set xOutApp = CreateObject("Outlook.Application")
For i = 1 To xLastRow
xRgDateVal = ""
xRgDateVal = xRgDate.Offset(i - 1).Value
If xRgDateVal <> "" Then 'Checks for blank cells
If CDate(xRgDateVal) = Date Then 'Checks if date = today's date
If xIsDateANumber <> IsDate(xRgDateVal) Then 'Checks if date is a valid date
xMailSubject = "The following Tasks are due today: " & xRgDateVal
vbCrLf = "
"
xMailBody = "******>"
xMailBody = xMailBody & "Attention! " & vbCrLf
xMailBody = xMailBody & xRgDepartment.Offset(i - 1).Value & " " & xRgTask.Offset(i - 1).Value & " is due today!" & vbCrLf
xMailBody = xMailBody & ""
Set xMailItem = xOutApp.CreateItem(0)
With xMailItem
.Subject = xMailSubject
.To = "1st_email@domain.com"
.Cc = "2nd_email@domain.com"
.HTMLBody = xMailBody
.Display
'.Send
End With
Set xMailItem = Nothing
End If 'End IsDate IF
End If 'End today's date IF
End If 'End blank cell IF
Next
Set xOutApp = Nothing
End Sub
Thanks in advance!