excelbytes
Active Member
- Joined
- Dec 11, 2014
- Messages
- 291
- Office Version
- 365
- Platform
- Windows
I have a spreadsheet that has a list of due dates in column B. In column D is where the project is marked complete. Column A has the project name. I would like an e-mail sent via Outlook each time the workbook is opened for each project where the date in column B is past today and column D is not marked "Complete". I found this code, but it doesn't seem to generating an e-mail:
Sub Send_Email()
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 = "runninrep@outlook.com"
sSendCC = ""
sSendBCC = ""
sSubject = "Project(s) Past Due!"
lLastRow = Cells(Rows.Count, 1).End(xlUp).Row
For lRow = 2 To lLastRow
If Cells(lRow, 4) <> "COMPLETED" Then
If Cells(lRow, 2) <= 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 passed! "
sTemp = sTemp & "for this project:" & vbCrLf & vbCrLf
' Assumes project name is in column B
sTemp = sTemp & " " & Cells(lRow, 1)
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
.Send
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
I also found this code:
Option Explicit
Sub SendEmail03()
Dim Date_Range As Range
Dim rng As Range
Set Date_Range = Range("B4:B203")
For Each rng In Date_Range
If rng.Value <= Date Then
Dim Subject, Send_From, Send_To, _
Cc, Bcc, Body As String
Dim Email_Obj, Single_Mail As Variant
Subject = "See Past Due Dates"
Send_From = "runninrep@outlook.com"
Send_To = "runninrep@outlook.com"
Body = "Check file for past due dates"
On Error GoTo debugs
Set Email_Obj = CreateObject("Outlook.Application")
Set Single_Mail = Email_Obj.CreateItem(0)
With Single_Mail
.Subject = Subject
.To = Send_To
.Body = Body
.send
End With
End If
Next
Exit Sub
debugs:
If Err.Description <> "" Then MsgBox Err.Description
End Sub
Sadly, I'm not sure if there is an issue with the code or if I just don't have the connection from Excel to Outlook set up properly. Do you know of any instructions to ensure this is done correctly?
Thanks in advance for any help you can provide.
Sub Send_Email()
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 = "runninrep@outlook.com"
sSendCC = ""
sSendBCC = ""
sSubject = "Project(s) Past Due!"
lLastRow = Cells(Rows.Count, 1).End(xlUp).Row
For lRow = 2 To lLastRow
If Cells(lRow, 4) <> "COMPLETED" Then
If Cells(lRow, 2) <= 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 passed! "
sTemp = sTemp & "for this project:" & vbCrLf & vbCrLf
' Assumes project name is in column B
sTemp = sTemp & " " & Cells(lRow, 1)
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
.Send
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
I also found this code:
Option Explicit
Sub SendEmail03()
Dim Date_Range As Range
Dim rng As Range
Set Date_Range = Range("B4:B203")
For Each rng In Date_Range
If rng.Value <= Date Then
Dim Subject, Send_From, Send_To, _
Cc, Bcc, Body As String
Dim Email_Obj, Single_Mail As Variant
Subject = "See Past Due Dates"
Send_From = "runninrep@outlook.com"
Send_To = "runninrep@outlook.com"
Body = "Check file for past due dates"
On Error GoTo debugs
Set Email_Obj = CreateObject("Outlook.Application")
Set Single_Mail = Email_Obj.CreateItem(0)
With Single_Mail
.Subject = Subject
.To = Send_To
.Body = Body
.send
End With
End If
Next
Exit Sub
debugs:
If Err.Description <> "" Then MsgBox Err.Description
End Sub
Sadly, I'm not sure if there is an issue with the code or if I just don't have the connection from Excel to Outlook set up properly. Do you know of any instructions to ensure this is done correctly?
Thanks in advance for any help you can provide.