See it in Action!Watch this video to see this macro in action.
The CodeHere is the code for this macro. Make sure the following References are setup before running it: Visual Basic For Applications, Microsoft Outlook 16.0 Object Library
'Leverage & Lean "Less Clicks, More Results" Sub GreetingGoodbyeContacts() ' Means that these variables are in use Dim ContactEmailAddress As String ' Dim ContactItem As Object ' Dim ContactItems As Items ' Dim ContactName As String ' Dim EmailCurrent As Outlook.MailItem ' Dim Goodbye As String ' Dim Greeting As String ' Dim GreetingGoodbyeContacts As String ' Dim objApp As Application ' Dim objNS As NameSpace ' Dim olDocument As Word.Document ' Dim olInspector As Outlook.Inspector ' Dim olSelection As Word.Selection ' Dim Recipient As Recipient Dim RecipientEmail As Object Dim RecipientEmailAddress As String ' Dim TimeofDay As Date ' Dim Weekday As String ' On Error GoTo LeverageLean Set olInspector = Application.ActiveInspector() Set RecipientEmail = olInspector.CurrentItem Weekday = Format(Date, "dddd") 'What day of the week is it? TimeofDay = Time() 'What time is it now? 'Custom Greeting and Goodbye based on answers If TimeofDay < "9:00:00 AM" Then Greeting = "Good Morning" ElseIf Weekday = "Friday" And TimeofDay > "3:30:00 PM" Then Greeting = "Hello" Goodbye = "Enjoy your weekend!" ElseIf TimeofDay > "3:30:00 PM" Then Greeting = "Hello" Goodbye = "Have a great night!" ElseIf Greeting = "" Then Greeting = "Hello" End If For Each Recipient In RecipientEmail.Recipients 'Loop through all the Recipients If Recipient.Type = olTo And RecipientEmailAddress = "" Then 'If the Recipient is in the To field RecipientEmailAddress = Recipient.Address 'Capture the first Recipient listed in the To field End If Next Recipient Set objApp = CreateObject("Outlook.Application") Set objNS = objApp.GetNamespace("MAPI") Set ContactItems = objNS.GetDefaultFolder(olFolderContacts).Items 'Set Contacts Folder ContactItems.SetColumns ("Email1Address, FirstName") 'Set Columns to only look to a Contact's Primary Email Address and First Name value For Each ContactItem In ContactItems 'Loop through the Contacts Items ContactEmailAddress = ContactItem.Email1Address 'Set the Contact's Email Address If StrComp(ContactEmailAddress, RecipientEmailAddress, vbTextCompare) = 0 Then 'If the Contact's Email equals the Recipient's Email ContactName = ContactItem.FirstName 'Set the ContactName with the Contact's First Name End If Next If ContactName = "" Then 'If no Contact Name is found GreetingGoodbyeContacts = Greeting & "," & vbNewLine & vbNewLine & Goodbye ElseIf ContactName <> "" Then 'If a Contact Name is found GreetingGoodbyeContacts = Greeting & " " & ContactName & "," & vbNewLine & vbNewLine & Goodbye End If Set olDocument = olInspector.WordEditor Set olSelection = olDocument.Application.Selection olSelection.TypeText GreetingGoodbyeContacts 'Enter Hello Contacts at the beginning of the email Set ContactItem = Nothing Set ContactItems = Nothing Set objApp = Nothing Set objNS = Nothing Set olDocument = Nothing Set olInspector = Nothing Set olSelection = Nothing Exit Sub LeverageLean: MsgBox (Err.Number & " - " & Err.Description & vbNewLine & vbNewLine & "Don't hesitate to email me: email@example.com") End Sub 'Stay Awesome!
Macro MondayHere is the Macro Monday video this macro was featured in. Watch this video to learn how to get the most out of this macro and start using it today!
CustomizationThese lines of code can be customized to personalize this macro. These lines of code can be customized to personalize this macro.
32 through 41: Update the TimeofDay to get your Greeting and Goodbye to fire at specific times. You can also indicate certain Greetings or Goodbyes to be generated based on the Weekday within these lines of code.
62 through 65: GreetingGoodbyeContacts is the final string that will be entered into your email body. Here is where you can update spacing by adding or removing vbNewLine.
Although you can create a custom button to trigger the Greeting and Goodbye macro on command you can also automate this macro with some additional VBA code.
Here is the additional code that can you used to fire this macro with Outlook’s Reply and Reply All buttons. Copy and Paste this VBA code into your Outlook Session and restart Outlook. You must make your GreetingGoodbye macro Public so it can be called within your Outlook Session.
'Leverage & Lean "Less Clicks, More Results" Option Explicit Private WithEvents oExpl As Explorer Private WithEvents oItem As MailItem Dim bDiscardEvents As Boolean Dim oResponse As MailItem Private Sub Application_Startup() Set oExpl = Application.ActiveExplorer bDiscardEvents = False 'Call the Macros listed below: 'Call MacroName End Sub Private Sub oExpl_SelectionChange() On Error Resume Next Set oItem = oExpl.Selection.Item(1) End Sub Sub oItem_Reply(ByVal Response As Object, Cancel As Boolean) On Error GoTo LeverageLean Cancel = True bDiscardEvents = True 'Display current email selection Set oResponse = oItem.Reply oResponse.Display 'Call the Macros listed below: 'Call MacroName Exit Sub LeverageLean: End Sub Sub oItem_ReplyAll(ByVal Response As Object, Cancel As Boolean) On Error GoTo LeverageLean Cancel = True bDiscardEvents = True 'Display current email selection Set oResponse = oItem.ReplyAll oResponse.Display 'Call the Macros listed below: 'Call MacroName Exit Sub LeverageLean: End Sub 'Stay Awesome!