Copy this to your workbook module
Private Sub Workbook_Open()
Application.OnTime TimeValue("08:00:00"), "SomethingElse"
End Sub
And copy this to its own module:
Public rngEmail As Range
Public Sub SomethingElse()
Dim l As Long
Dim lRow As Long
lRow = Range("H" & Rows.Count).End(xlUp).Row
For l = 1 To lRow
If DateAdd("d", 29, Range("H" & l).Value) = Date Then
Set rngEmail = Range("A" & l & ":B" & l)
Call SendEmail
End If
Next l
End Sub
Sub SendEmail()
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set rng = Nothing
Set rng = rngEmail
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = "JohnDoe@myemail.com"
.CC = ""
.BCC = ""
.Subject = "This is the status of your tasks"
.HTMLBody = RangetoHTML(rng)
'.Send
.display 'remove ' to display instead of send
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Function RangetoHTML(rng As Range)
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook
TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
rng.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
.Cells(1).Select
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With
With TempWB.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=TempWB.Sheets(1).Name, _
Source:=TempWB.Sheets(1).UsedRange.Address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.ReadAll
ts.Close
RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
"align=left x:publishsource=")
TempWB.Close savechanges:=False
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function
This will run on the ACTIVE SHEET at 8 AM on every day that the workbook is open. The workbook has to be open for this macro to function.
Bookmarks