send auto email work anniversary

kerm007

Active Member
Joined
Mar 16, 2019
Messages
253
Office Version
  1. 365
Platform
  1. Windows
hello
how can my HR girls auto send email to all when its the work anniversary of some one from an excel sheet populated with the name and the anniversay date of an employee ?
Thanks
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
I redid the whole thing, so I'll attempt to lay out the entire construction in one well-worded go. Wish me luck!

[WORKSHEET SET UP]

Cell B2 is a named range (nrToday) and its formula is
Excel Formula:
=TODAY()

The data table is named tblX with its headers on Row 3.

Column A contains employee names, Column B shows their hire dates, and Column C lists their email addresses.

Columns D and E are used to calculate whether the people have an anniversary and how many years they've been with the company.

The formula for Cell D4 is
Excel Formula:
=IF(AND(MONTH(B4)=MONTH(nrToday),DAY(B4)=DAY(nrToday)),"Anniversary","")

The formula for Cell E4 is
Excel Formula:
=DATEDIF([@Hired],nrToday,"Y")

[CODE SETUP]

From the VB Editor, paste this code into the ThisWorkbook object:
VBA Code:
Option Explicit

Private Sub Workbook_Open()

Dim rngCell     As Range
Dim strMessage  As String


For Each rngCell In Range("tblX[Name]")

  If rngCell.Offset(0, 3) = "Anniversary" Then

    strMessage = "Congratulations, yo!<br><br>" & _
      "We are so totally chuffed to have " & _
      "you working with us.<br><br>Happy " & _
      Addth(rngCell.Offset(0, 4).Value) & _
      " Work Anniversary!"

    Call Email _
      (rngCell.Offset(0, 2), "Happy Anniversary!", strMessage)

    strMessage = ""

  End If

Next rngCell

End Sub

And paste these codes into Module1:
VBA Code:
Option Explicit

Function Addth(pNumber As String) As String
'Updateby20141027
'Function Posted At:
'https://www.extendoffice.com/documents/excel/2167-excel-convert-cardinal-to-ordinal.html


Select Case CLng(VBA.Right(pNumber, 1))

  Case 1:  Addth = pNumber & "st"
  Case 2:  Addth = pNumber & "nd"
  Case 3:  Addth = pNumber & "rd"
  Case Else: Addth = pNumber & "th"

End Select

Select Case VBA.CLng(VBA.Right(pNumber, 2))

  Case 11, 12, 13:  Addth = pNumber & "th"

End Select

End Function


Sub Email _
  (sTo As String, sSub As String, _
   sHTML As String, Optional sAttach As Variant, _
   Optional sAction As String = "Display", _
   Optional sCCs As String, Optional sSender As String)
'v1.49   31 Mar 2023
'https://www.RONDEBRUIN.nl/win/s1/outlook/signature.htm
'with appreciation to ChatGPT for help with the attachment
'logic


'Declare Variables
Dim lngLoop  As Long
Dim olApp    As Object
Dim olMsg    As Object
Dim arrEh    As Variant

'Create Email Message
Set olApp = CreateObject("Outlook.Application")
Set olMsg = olApp.CreateItem(0)

On Error Resume Next

  Set olApp = GetObject(, "Outlook.Application")
 
  If olApp Is Nothing Then _
    Set olApp = CreateObject("Outlook.Application")
 
  Set olMsg = olApp.CreateItem(0)
 
  With olMsg
 
    .Display

    If sSender <> "" Then

      .SentOnBehalfOfName = sSender

    End If

    .To = sTo
    .CC = sCCs
    .HTMLBody = sHTML & .HTMLBody
    .Subject = sSub

    If Not IsMissing(sAttach) And Not IsNull(sAttach) Then

      If Not IsArray(sAttach) Then

        .Attachments.Add sAttach
        GoTo Utah

      End If
  
      For lngLoop = LBound(sAttach) To UBound(sAttach)
    
        .Attachments.Add sAttach(lngLoop)
    
      Next lngLoop

Utah:
    End If
  
    If sAction = "Display" Then
  
      .Display
  
    ElseIf sAction = "Send" Then
  
      .Send
  
    End If
 
  End With
On Error GoTo 0

'Clear Set Variables
Set olApp = Nothing
Set olMsg = Nothing


End Sub

I tested it out and it all worked, so once you plug in your employee data, save and close the file, it should work nicely the next time someone opens the file. (y)
1686774438063.png
 
Last edited:
Upvote 1
Solution
If you set up your spreadsheet something along the lines of what I have prepared, you can do that.

First, I created a named range (a cell with the TODAY formula in it so it will always reflect the current date).

Next, I made a table with employee names, dates of birth, email addresses, and an extra column to identify any birthday people. Here's the formula for comparing birthdates:

Excel Formula:
=IF(AND(MONTH(B4)=MONTH(nrToday),DAY(B4)=DAY(nrToday)),"Birthday person!","")

Next, I crafted this macro which will launch upon opening the file:

VBA Code:
Private Sub Workbook_Open()

Dim rngCell As Range

For Each rngCell In Range("tblPeeps[Name]")

  If rngCell.Offset(0, 3) = "Birthday person!" Then

    Call Email _
      (rngCell.Offset(0, 2), "Happy Birthday!", "For you're a jolly good fellow, folderol lorem ipsum!")

  End If

Next rngCell

End Sub

You will need to either add a code module or include this macro in your existing one for the previous code to function:

VBA Code:
Sub Email _
  (sTo As String, sSub As String, _
   sHTML As String, Optional sAttach As Variant, _
   Optional sAction As String = "Display", _
   Optional sCCs As String, Optional sSender As String)
'v1.49   31 Mar 2023
'Crafted by Ron DeBruin, Wookiee at MrExcel, and ChatGPT
'https://www.RONDEBRUIN.nl/win/s1/outlook/signature.htm



'Declare Variables
Dim lngLoop  As Long
Dim olApp    As Object
Dim olMsg    As Object
Dim arrEh    As Variant

'Create Email Message
Set olApp = CreateObject("Outlook.Application")
Set olMsg = olApp.CreateItem(0)

On Error Resume Next

  Set olApp = GetObject(, "Outlook.Application")
  
  If olApp Is Nothing Then _
    Set olApp = CreateObject("Outlook.Application")
  
  Set olMsg = olApp.CreateItem(0)
  
  With olMsg
  
    .Display

    If sSender <> "" Then

      .SentOnBehalfOfName = sSender

    End If

    .To = sTo
    .CC = sCCs
    .HTMLBody = sHTML & .HTMLBody
    .Subject = sSub

    If Not IsMissing(sAttach) And Not IsNull(sAttach) Then

      If Not IsArray(sAttach) Then

        .Attachments.Add sAttach
        GoTo Utah

      End If
    
      For lngLoop = LBound(sAttach) To UBound(sAttach)
      
        .Attachments.Add sAttach(lngLoop)
      
      Next lngLoop

Utah:
    End If
    
    If sAction = "Display" Then
    
      .Display
    
    ElseIf sAction = "Send" Then
    
      .Send
    
    End If
  
  End With
On Error GoTo 0

'Clear Set Variables
Set olApp = Nothing
Set olMsg = Nothing


End Sub


1686686125399.png
 
Upvote 0
hello thanks
i did one with instruction but its not working so far :)
will continue to test
Thanks
 
Upvote 0
hello thanks
i did one with instruction but its not working so far :)
will continue to test
Thanks
I re-read my message and it occurred to me that I failed to mention that the Workbook_Open macro needs to be put in the worksheet itself, not the code module. I hope that helps.
 
Upvote 0
I re-read my message and it occurred to me that I failed to mention that the Workbook_Open macro needs to be put in the worksheet itself, not the code module. I hope that helps.
To be clear, it needs to go in the "ThisWorkbook" module, not any of the Sheet modules or in any General module that you insert.
 
Upvote 0
ok i insert formula into the B1,2 cell
the first vba code into this workbook
where do i insert the second VBA?
for now even the date is not working :)
Thanks helping
 
Upvote 0
also how can i adjust it to be the employment birthday like happy 2 years etc ?
 
Upvote 0
I apologize for my poorly worded response previously. I meant workbook but typed worksheet like a dipsheet. ☺️

I hope this helps clarify the location of the codes.

1686749691345.png
 
Upvote 0

Forum statistics

Threads
1,215,909
Messages
6,127,670
Members
449,397
Latest member
Bastbog

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