I have a w book that generates and auto sends emails depending on the due date and
if yes is in column k in all sheets.
It sends the email to the email in the code and what is in column e.
This is saved on a shared drive as more than one person needs to open it.
The problem is that it generates an e mail(s) each time it is opened , so if 3 different people open it
and there are 10 overdue items it would send 30 e mails.
What i would like it to do is to send the first batch of emails when the file is opened then may be
add todays date in another cell so that if it is opened on the same day no further mails are sent, but
if it is opened on the next day it would check the date realise either the date is blank or post and send
another batch of emails and so on.
For some reason i could not past the word book but i have added a drop box link and my code is below
https://www.dropbox.com/s/dspa…pread%20Sheet%20test.xlsm
Private Sub Workbook_Open()
Dim rngbody As Range
Dim c As Variant
Dim ddiff As Long
Dim mdiff As Long
Dim body As String
Dim w As Worksheet
Dim j As Integer
Dim cell As Range
Dim strto As String
Dim frRowCol As String
For Each w In Worksheets
If w.CodeName <> "Sheet2" Then
strto = ""
For Each cell In w.Range("A3:k200")
If cell.Value Like "?*@?*.?*" And LCase(cell.Offset(0, 6).Value) = "yes" Then
strto = strto & cell.Value & ";"
End If
Next cell
If Len(strto) > 0 Then strto = Left(strto, Len(strto) - 1)
Application.EnableEvents = False
'///New Code
Set rngbody = w.Range("A2").Resize(1, 7)
'///
' Set the initial email body. We will use this to check if we have issues later.
body = "The Following items are Due or Overdue Inspection" & ": " & Chr(9) & Chr(9) & Chr(9) & vbCrLf & vbCrLf & vbCrLf
For Each c In w.Range("F4:F" & w.Cells(Rows.Count, 6).End(xlUp).Row)
' If each value in the range is a date, then do a check for months and days.
If IsDate(c.Value) Then
ddiff = DateDiff("d", Now(), c.Value)
mdiff = DateDiff("m", Now(), c.Value)
' If greater than 1 month away, only populate the cell, do not add to the email
' body. You many want to change this.
If ddiff > 0 Then
c.Offset(0, 1).Value = ddiff & " Days From Now"
Else
' Else, if we have less than one month, use days as the indicator, and
' increment the email body.
If ddiff > 1 Then
c.Offset(0, 1).Value = ddiff & " Days From Now"
body = body & c.Offset(0, -4) & ": " & Chr(9) & c.Offset(0, -3) & Chr(9) & Chr(9) & Chr(9) & ddiff & " Days from now" & vbCrLf
Else
If ddiff = 0 Then
c.Offset(0, 1).Value = "Due Today"
body = body & c.Offset(0, -4) & ": " & Chr(9) & c.Offset(0, -5) & ": " & Chr(9) & Chr(9) & c.Offset(0, -3) & ": " & Chr(9) & c.Offset(0, -1) & ": " & Chr(9) & " Due today" & ": " & vbCrLf
Else
c.Offset(0, 1).Value = ddiff * -1 & " Days Overdue"
body = body & c.Offset(0, -4) & ": " & Chr(9) & c.Offset(0, -5) & ": " & Chr(9) & c.Offset(0, -3) & ": " & Chr(9) & c.Offset(0, -1) & ": " & Chr(9) & ddiff * -1 & " Days overdue" & ": " & vbCrLf
'///New Code
Set rngbody = Union(rngbody, c.Offset(0, -5).Resize(1, 7))
'///
End If
End If
End If
End If
Next c
'///New Code
If rngbody.Rows.Count > 1 Or rngbody.Areas.Count > 1 Then
rngbody.Copy Sheet2.Range("A1")
'///
sn = Sheet2.Range("A1").CurrentRegion
c01 = "<TABLE BORDER= 1 BORDERCOLOR=#525fb3 >"
On Error Resume Next
For j = 1 To UBound(sn)
If j = 1 Then frRowCol = " bgcolor=#a9bcf5 " Else If Application.IsOdd(j) Then frRowCol = " bgcolor=#f2f5a9" Else frRowCol = " bgcolor=#f5ecce"
c01 = c01 & "<tr" & frRowCol & " ><td>" & Join(Application.Index(sn, j), "</td><td>") & "</td></tr>"
Next j
c01 = c01 & "</table><P></P><P></P>"
On Error GoTo 0 '//Resets the error handler to break code in the event of an error
With CreateObject("Outlook.Application").CreateItem(0)
.To = strto
.cc = ""
.bcc = "[EMAIL="[email protected]"][email protected][/EMAIL]"
.Subject = "Northside Leeds Overdue " & w.Name & " Safety Checks "
.HTMLBody = "<STRONG>The Items In The Table Below Are Overdue Please Complete and Update Spread Sheet A.S.A.P </STRONG>" & c01
.display
End With
Sheet2.Cells(1).CurrentRegion.Offset(1).ClearContents
End If
End If
Next w
Application.EnableEvents = True
End Sub
Display More
Any help would be appreciated
Cheers
Peter