I have been trying to get a macro working to send an email when a date is added to a certain column in Excel. I thought I had found the right code. But it is not sending the email when I test it. How do I set this up? I would like an email to go out to the address in a cell when another cell is updated. This is the code I am trying to use. But I am not getting the email.
Sub SendMail()
Dim OutApp As Object
Dim OutMail As Object
Dim RelDate As Range
Dim lastRow As Long
Dim dateCell, dateCell1 As Date
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
lastRow = Range("A" & Rows.Count).End(xlUp).Row
On Error GoTo cleanup
For Each RelDate In Range("M2:M" & lastRow)
If RelDate = "" Then GoTo 1
dateCell = RelDate.Value
dateCell1 = Cells(RelDate.Row, "L").Value
If dateCell <> dateCell1 Then
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = Cells(RelDate.Row, "K").Value
.Subject = "Release Date Changed"
.Body = "Dear " & Cells(RelDate.Row, "A").Value _
& vbNewLine & vbNewLine & _
"The release date of " & Cells(RelDate.Row, "H").Value & _
" is changed to " & dateCell _
& vbNewLine & vbNewLine _
& vbNewLine & vbNewLine & _
"Regards," & vbNewLine & _
"Your Name"
.send
End With
On Error GoTo 0
Set OutMail = Nothing
End If
Cells(RelDate.Row, "L").Value = dateCell
RelDate.ClearContents
1: Next RelDate
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub
Sub SendMail()
Dim OutApp As Object
Dim OutMail As Object
Dim RelDate As Range
Dim lastRow As Long
Dim dateCell, dateCell1 As Date
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
lastRow = Range("A" & Rows.Count).End(xlUp).Row
On Error GoTo cleanup
For Each RelDate In Range("M2:M" & lastRow)
If RelDate = "" Then GoTo 1
dateCell = RelDate.Value
dateCell1 = Cells(RelDate.Row, "L").Value
If dateCell <> dateCell1 Then
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = Cells(RelDate.Row, "K").Value
.Subject = "Release Date Changed"
.Body = "Dear " & Cells(RelDate.Row, "A").Value _
& vbNewLine & vbNewLine & _
"The release date of " & Cells(RelDate.Row, "H").Value & _
" is changed to " & dateCell _
& vbNewLine & vbNewLine _
& vbNewLine & vbNewLine & _
"Regards," & vbNewLine & _
"Your Name"
.send
End With
On Error GoTo 0
Set OutMail = Nothing
End If
Cells(RelDate.Row, "L").Value = dateCell
RelDate.ClearContents
1: Next RelDate
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub