'---------------------
Sub FindMyDates()
'---------------------
Dim iRows As Long, iFldNum As Long, iResultOFF As Long
Dim vTxt, vEmail, vBody
Dim sResultCol As String
Dim i As Integer
Dim vDate2Find As Date
Const kResultHdr = "Results"
Const kFOUND = "found"
Const kColDTE = 4 'offset val, aka col.5
Const kColEMAIL = 5 'offset val aka col.6
vBody = "This is the body of the email"
'load the legal search values
vDate2Find = get6MoDate()
'add a result column
Sheets("data").Activate
Range("A1").Select
Selection.End(xlToRight).Select
If InStr(ActiveCell.Value, kResultHdr) = 0 Then
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = kResultHdr
End If
iFldNum = ActiveCell.Column
iResultOFF = iFldNum - Range("A1").Column
sResultCol = iFldNum & ":" & iFldNum
sResultCol = getMyColLtr()
'clear results col.
Columns(iFldNum).ClearContents
Range(sResultCol & "1").Value = kResultHdr
'get #rows
Range("A1").Select
iRows = ActiveSheet.UsedRange.Rows.Count
'MsgBox iRows
Range("A2").Select
While ActiveCell.Row <= iRows
vTxt = ActiveCell.Offset(0, kColDTE).Value
If CDate(vTxt) = vDate2Find Then
ActiveCell.Offset(0, iResultOFF).Value = kFOUND
vEmail = ActiveCell.Offset(0, kColEMAIL).Value
'Debug.Print vEmail
Send1Email vEmail, "Your acct", vBody
End If
ActiveCell.Offset(1, 0).Select 'next row
Wend
'filter results
ActiveSheet.Range("A1").AutoFilter Field:=iFldNum, Criteria1:=kFOUND
'copy the results
'SaveFoundData
End Sub
'---------------------
Public Function getMyColLtr()
'---------------------
Dim vRet
Dim i As Integer
vRet = Mid(ActiveCell.Address, 2)
i = InStr(vRet, "$")
If i > 0 Then vRet = Left(vRet, i - 1)
getMyColLtr = vRet
End Function
'---------------------
private Sub SaveFoundData()
'---------------------
Range("A1").Select
ActiveSheet.UsedRange.Select
'Range("A1:G27").Select
Selection.Copy
Workbooks.Add
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveWorkbook.SaveAs Filename:="C:\TEMP\found data.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
End Sub
Public Function get6MoDate()
get6MoDate = DateAdd("m", -6, Date)
End Function
Public Function Send1Email(ByVal pvTo, ByVal pvSubj, ByVal pvBody, Optional ByVal pvFile) As Boolean
Dim oApp As Outlook.Application
Dim oMail As Outlook.MailItem
On Error GoTo ErrMail
'NOTE : YOU MUST HAVE THE OUTLOOK REFERENCE CHECKED IN VBE; Alt-F11, menu,tools, references, Microsoft Outlook XX Object library
Set oApp = GetApplication("Outlook.Application") 'it may be open already so use this
'Set oApp = CreateObject("Outlook.Application") 'not this
Set oMail = oApp.CreateItem(olMailItem)
With oMail
.To = pvTo
.Subject = pvSubj
If Not IsMissing(pvFile) Then .Attachments.Add pvFile, olByValue, 1
.HTMLBody = pvBody
'If Not IsNull(pvBody) Then .Body = pvBody
.Display True 'show user but dont send yet
'.Send 'send now
End With
Send1Email = True
endit:
Set oMail = Nothing
Set oApp = Nothing
Exit Function
ErrMail:
MsgBox Err.Description, vbCritical, Err
Resume endit
'DoCmd.OutputTo acOutputReport, "rMyReport", acFormatPDF, vFile
End Function
Private Function GetApplication(className As String) As Object
' function to encapsulate the instantiation of an application object
Dim theApp As Object
On Error Resume Next
Set theApp = GetObject(, className)
If Err.Number <> 0 Then
MsgBox "Unable to Get" & className & ", attempting to CreateObject"
Set theApp = CreateObject(className)
End If
If theApp Is Nothing Then
Err.Raise Err.Number, Err.Source, "Unable to Get or Create the " & className & "!"
Set GetApplication = Nothing
End If
'MsgBox "Successfully got a handle on Outlook Application, returning to caller"
Set GetApplication = theApp
End Function