Option Explicit
Option Compare Text
Private Sub Workbook_Open()
Dim ws As Worksheet
Dim cel As Range, Rng As Range
Dim lrow As Long, StDte As Long, EndDte As Long, MyCount As Long, MNum As Long
Dim mystr As String
Set ws = Sheets("Sheet1") ' change to be your worksheet name
StDte = Date
EndDte = DateAdd("M", 4, Date)
lrow = ws.Cells(Rows.count, 1).End(xlUp).row
MNum = WorksheetFunction.CountA(Range("M:M"))
Set Rng = ws.Range("A1:M" & lrow)
ws.AutoFilterMode = False
Debug.Print Rng.address
Rng.AutoFilter Field:=10, Criteria1:=">=" & StDte, Operator:=xlAnd, Criteria2:="<=" & EndDte
If MNum <> 0 Then
Rng.AutoFilter Field:=13, Criteria1:="<>Email"
Else
End If
MyCount = ws.AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).count
If MyCount > 1 Then
For Each cel In ws.UsedRange.SpecialCells(xlCellTypeVisible).Columns(1).Cells
If cel.row <> 1 Then
ws.Cells(cel.row, "M") = "Email Sent"
End If
Next cel
ws.UsedRange.SpecialCells(xlCellTypeVisible).Resize(, 8).Copy
Dim Outlook As Object, newEmail As Object, xInspect As Object, pageEditor As Object
Dim wdformatplaintext As Long
Set Outlook = CreateObject("Outlook.Application")
Set newEmail = Outlook.CreateItem(0)
Set newEmail = Outlook.CreateItem(0)
With newEmail
.To = "CHANGE THIS TO YOUR RECIPIENT EMAIL ADDRESS" ' change as required
.subject = "Defect Handovers Due"
.ReadReceiptRequested = False
.htmlbody = "Your defects handover date is due 4 months from today for" & "<br><br>"
.display
Set xInspect = newEmail.GetInspector
Set pageEditor = xInspect.WordEditor
pageEditor.Application.Selection.Start = Len(.htmlbody)
pageEditor.Application.Selection.End = pageEditor.Application.Selection.Start
pageEditor.Application.Selection.pasteandformat (wdformatplaintext)
.display
.htmlbody = .htmlbody & "<br><br>" & _
"Please action and contact client."
Set pageEditor = Nothing
Set xInspect = Nothing
Application.CutCopyMode = False
.display ' change this to .send once you are happy with the setup to auto send email
End With
Else
End If
End Sub