Sending email based on date criteria.

Arun Kumar MA

Board Regular
Joined
Jan 10, 2012
Messages
122
Hi all,

Iam using the below coding to trigger email to the receipient if the difference b/w todays date & DOJ is more than 104 days.


The mail gets triggerd but with below issues:
  • The email body & email body 1 are drafted in the same even after using vbNewLine.
  • I want a single mail to be sent to the supervisor for all the satisfying criterias as on that particular date, below is the header for reference.
  • Also I need a signature to be added.
<TABLE style="WIDTH: 599pt; BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 cellPadding=0 width=795><COLGROUP><COL style="WIDTH: 31pt; mso-width-source: userset; mso-width-alt: 1499" width=41><COL style="WIDTH: 50pt; mso-width-source: userset; mso-width-alt: 2413" width=66><COL style="WIDTH: 83pt; mso-width-source: userset; mso-width-alt: 4022" span=2 width=110><COL style="WIDTH: 74pt; mso-width-source: userset; mso-width-alt: 3584" width=98><COL style="WIDTH: 139pt" span=2 width=185><TBODY><TR style="HEIGHT: 15.75pt" height=21><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: #1f497d; WIDTH: 31pt; HEIGHT: 15.75pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: windowtext 0.5pt solid" class=xl67 height=21 width=41>Sl No</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: #1f497d; WIDTH: 50pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: windowtext 0.5pt solid" class=xl67 width=66>Emp Id</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: #1f497d; WIDTH: 83pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: windowtext 0.5pt solid" class=xl67 width=110>Employee Name</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: #1f497d; WIDTH: 83pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: windowtext 0.5pt solid" class=xl67 width=110>Date of joining</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: #1f497d; WIDTH: 74pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: windowtext 0.5pt solid" class=xl67 width=98>Mail Dated</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: #1f497d; WIDTH: 139pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: windowtext 0.5pt solid" class=xl67 width=185>Immediate Supervisor</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: #1f497d; WIDTH: 139pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: windowtext 0.5pt solid" class=xl67 width=185>Mailing ID</TD></TR></TBODY></TABLE>


Code:
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
Range("H2").Value = Date
Dim myOutlook As Object
Dim myMailItem As Object
Dim FName As String
Dim DOJ As String
Dim Emailid As String
Dim i As Long
Dim PM As Range
Dim Email_Subject, Email_Send_From, Email_Send_To, _
    Email_Cc, Email_Bcc, Email_Body, Email_Body1, Email_Body2, Email_Body3 As String
 
Worksheets("Sheet1").Activate
Home_Last_Row = Worksheets("Sheet1").Cells(Rows.Count, 3).End(xlUp).Row
For i = 3 To Home_Last_Row
    If Now() > Range("D" & i) + 104 Then
    'If Range("E" & i) = Range("H2") Then
        FName = Range("F" & i)
        Emailid = Range("G" & i)
        DOJ = Range("D" & i)
        Exit For
    End If
Next i
If DOJ = "" Then Exit Sub
Set otlApp = CreateObject("Outlook.Application")
Set otlNewMail = otlApp.CreateItem(olMailItem)
CellRow = ActiveCell.Row
CellColumn = ActiveCell.Column
Email_Body = vbNewLine & "Dear " & FName & "," & vbNewLine & vbNewLine
Email_Body1 = vbNewLine & "The below mentioned resources has/have joined your team, would you pls rate them on the scale given below." & vbNewLine & vbNewLine
'Cells(ActiveCell.Row, 3).Value
Set PM = ActiveSheet.Range("L2:M8")
 
'PM = ActiveSheet.Range("N11")
Email_Body2 = PM
'Email_Body2 = Selection.Paste '& vbNewLine & vbNewLine & "Many Thanks" & vbNewLine & "Prasad - HR Team"
 
 
With otlNewMail
    .To = Emailid
    '.CC = Cells(1, 1)
    .Subject = "Rate your team member."
    .HTMLBody = Email_Body & Email_Body1 & RangetoHTML(PM)  'Email_Body2
    .DeferredDeliveryTime = Range("H2")
    .Send
End With
Set otlNewMail = Nothing
Set otlApp = Nothing
Set otlAttach = Nothing
Set otlMess = Nothing
Set otlNSpace = Nothing
 
Application.ScreenUpdating = True
 
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"
 
    ' Copy the range and create a workbook to receive the data.
    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        '.Cells(1).Paste
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Columns("A:B").Select
        .Columns("A:F").AutoFit
        .Columns("A:A").ColumnWidth = 10
        .Columns("B:B").ColumnWidth = 15
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With
 
    ' Publish the sheet to an .htm file.
    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
 
    ' Read all data from the .htm file into the RangetoHTML subroutine.
    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=")
 
    ' Close TempWB.
    TempWB.Close savechanges:=False
 
    ' Delete the htm file.
    Kill TempFile
 
    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
 
End Function

Any assistance will be highly appreciated.

Thanks & Regards
Arun
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
To bring clarity to my second requirement.

For ex:
For todays date if 5 employees DOJ satisfies the criteria, and if the supervisor of all the 5 employees is same, I want a single mail to be sent to the supervisor with all the 5 names mentioned in the mail.
 
Upvote 0
Hi Dosnox,

There is a possibility that the supervisor for all the 5 employees might be same.
So instead of triggering 5 different emails to the supervisor, I would like to trigger just 1 email comprising of all the 5 satisfying employees name in it.

Regards
Arun
 
Upvote 0

Forum statistics

Threads
1,215,500
Messages
6,125,166
Members
449,210
Latest member
grifaz

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top