Advertisement
Donnycampo

CheckExpirationDates and Email

Jan 12th, 2024
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub ScheduleDailyCheck()
  2.     ' Schedule the CheckExpirationDates macro to run at a specific time each day
  3.    Application.OnTime TimeValue("08:00:00"), "CheckExpirationDates"
  4. End Sub
  5. Sub CheckExpirationDates()
  6.     Dim ws As Worksheet
  7.     Dim lastRow As Long
  8.     Dim currentRow As Long
  9.     Dim expDateJ As Date
  10.     Dim expDateK As Date
  11.     Dim emailSubject As String
  12.     Dim emailBody As String
  13.     Dim personInfo As Collection
  14.     Dim personKey As String
  15.  
  16.     ' Set the worksheet
  17.    Set ws = ThisWorkbook.Sheets("2024 Season") ' Change "Sheet1" to the actual sheet name
  18.    
  19.     ' Find the last row with data in column B
  20.    lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
  21.  
  22.     ' Initialize email body and collection for person information
  23.    emailBody = ""
  24.     Set personInfo = New Collection
  25.  
  26.     ' Loop through rows starting from 2 to the last row
  27.    For currentRow = 2 To lastRow
  28.         ' Check if expiration date in Column J is before today
  29.        If IsDate(ws.Cells(currentRow, 10).Value) Then
  30.             expDateJ = CDate(ws.Cells(currentRow, 10).Value)
  31.             ' Check if expiration date in Column K is before today
  32.            If IsDate(ws.Cells(currentRow, 11).Value) Then
  33.                 expDateK = CDate(ws.Cells(currentRow, 11).Value)
  34.                
  35.                 ' Check if either document is expired
  36.                If expDateJ < Date Or expDateK < Date Then
  37.                     ' Check if Column L is empty
  38.                    If IsEmpty(ws.Cells(currentRow, 12).Value) Then
  39.                         ' Insert "Emailed" in Column L
  40.                        ws.Cells(currentRow, 12).Value = "Emailed"
  41.                        
  42.                         ' Add information to the personInfo collection
  43.                        personKey = ws.Cells(currentRow, 2).Value
  44.                         If Not ContainsPerson(personInfo, personKey) Then
  45.                             personInfo.Add personKey, personKey
  46.                             ' Add information to the email body
  47.                            emailBody = emailBody & "Name: " & personKey & vbCrLf
  48.                         End If
  49.                         ' Add document information to the email body
  50.                        emailBody = emailBody & "Document Expired: " & _
  51.                                      IIf(expDateJ < Date, "RV Insurance", "") & _
  52.                                      IIf(expDateJ < Date And expDateK < Date, vbCrLf, "") & _
  53.                                      IIf(expDateK < Date, "Golf Cart Insurance", "") & vbCrLf & vbCrLf
  54.                     End If
  55.                 End If
  56.             End If
  57.         End If
  58.     Next currentRow
  59.  
  60.     ' Check if there are expired policies to notify about
  61.    If Len(emailBody) > 0 Then
  62.         ' Prepare email subject
  63.        emailSubject = "Expired Insurance Policies Notification"
  64.        
  65.         ' Send email using Outlook
  66.        SendEmail "Redacted", emailSubject, emailBody
  67.     End If
  68. End Sub
  69.  
  70. Function ContainsPerson(col As Collection, key As String) As Boolean
  71.     On Error Resume Next
  72.     ContainsPerson = (col(key) Is Nothing)
  73.     On Error GoTo 0
  74. End Function
  75.  
  76. Sub SendEmail(recipient As String, subject As String, body As String)
  77.     ' Send email using Outlook
  78.    Dim OutlookApp As Object
  79.     Dim MailItem As Object
  80.    
  81.     On Error Resume Next ' Ignore errors if Outlook is not open
  82.    
  83.     ' Attempt to get Outlook Application
  84.    Set OutlookApp = GetObject(, "Outlook.Application")
  85.    
  86.     On Error GoTo 0 ' Reset error handling
  87.    
  88.     If OutlookApp Is Nothing Then
  89.         ' If Outlook is not open, create a new instance
  90.        Set OutlookApp = CreateObject("Outlook.Application")
  91.     End If
  92.    
  93.     ' Create a new mail item
  94.    Set MailItem = OutlookApp.CreateItem(0)
  95.    
  96.     ' Fill in email details
  97.    MailItem.To = recipient
  98.     MailItem.subject = subject
  99.     MailItem.body = body
  100.    
  101.     ' Send the email
  102.    MailItem.Send
  103.    
  104.     ' Clean up objects
  105.    Set MailItem = Nothing
  106.     Set OutlookApp = Nothing
  107. End Sub
  108. Sub CancelScheduledCheck()
  109.     ' Cancel the scheduled CheckExpirationDates macro
  110.    On Error Resume Next
  111.     Application.OnTime TimeValue("08:00:00"), "CheckExpirationDates", , False
  112.     On Error GoTo 0
  113. End Sub
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement