VBA Macro used in the Microsoft Office Outlook Application

Outlook Greeting and Goodbye | VBA Macro #13

Share on facebook
Share on twitter
Share on linkedin
We are busy, sometimes too busy to type a friendly Greeting and Goodbye in our emails. This Outlook macro automates the entry of a Greeting “Good Morning” or “Hello” at the beginning of your emails. At the end of your email this macro automatically enters a Goodbye “Have a good night!” and “Enjoy your weekend!”. The appropriate entries are determined based on the current time and the day of the week. The Greeting and Goodbye macro helps you deliver better customer service and create better connections with your clients! This macro can be fired from a custom button or trigger automatically when replying to an email. The VBA code is easy to customize allowing you to edit or add additional Greetings and Goodbyes.

See it in Action!

Watch this video to see this macro in action.

The Code

Here is the code behind this macro.
'Leverage & Lean "Less Clicks, More Results"
Sub GreetingGoodbye()
' Means that these variables are in use
Dim EmailBody As String '
Dim Goodbye As String '
Dim Greeting As String '
Dim olDocument As Word.Document '
Dim olEmail As Outlook.MailItem '
Dim olInspector As Outlook.Inspector '
Dim olSelection As Word.Selection '
Dim TimeofDay As Date '
Dim Weekday As String '

On Error GoTo LeverageLean

'Select the current email
Set olInspector = Application.ActiveInspector()
Set olEmail = 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

'Create the text for the Email Body
EmailBody = Greeting & "," & vbNewLine & vbNewLine & Goodbye

'Paste the text into the beginning of the email
Set olDocument = olInspector.WordEditor
Set olSelection = olDocument.Application.Selection
olSelection.TypeText EmailBody
bDiscardEvents = False

Set olInspector = Nothing
Set olEmail = Nothing
Set olDocument = Nothing
Set olSelection = Nothing

Exit Sub

LeverageLean:
MsgBox ("Something went wrong. If you are automatically firing this macro try using a custom button.  Don't hesitate to email me: brentschneider@leveragelean.com")

End Sub
'Stay Awesome!

Macro Monday

Here 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!

Customization

These lines of code can be customized to personalize this macro.

24 through 33: 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.

37 EmailBody 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!
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments

Search for Outlook Macros

Recent Macros

VBA Macro used in the Microsoft Office Outlook Application

Outlook Send Same Email to Different Recipients | VBA Macro #31

To BCC or to not BCC?  That is the question that comes to mind when emailing a variety of different recipients.  This is especially important if you are working with multiple clients.  You can BCC recipients so they can’t click reply all. Recipients won’t see who was emailed but that is actually the problem.  The value is knowing who was emailed so customers know if additional follow-up is needed.  Instead you can CC recipients but once someone hits Reply All the conversation can get out of control.  This can create a bigger problem if the email thread is across multiple parties. Sending separate emails creates more value for the customer but how do you do this efficiently and effectively? You could create new emails do a lot of copying and pasting but this isn’t a sustainable process. Good news is there is always a better way with VBA! Our new Outlook macro makes sending the same email to different recipients so easy!

VBA Macro used in the Microsoft Office Excel Application

Excel Find and Replace across Multiple Word Documents | VBA Macro #30

Say a company rebranded and changed locations.  How would you handle updating all the necessary internal documents with the new company name, address, phone number, and email address? This can be a massive undertaking for any company which is why we created the Excel Find and Replace across Multiple Word Documents macro! This is an Excel macro that will Find and Replace values in Multiple Word Documents. This Excel macro is a great way to maintain Word Documents that are routinely changing information.

VBA Macro used in the Microsoft Office Word Application

Word Create Outlook Email with Selected Text | VBA Macro #29

Have you ever been prompted to send a follow up email while working in Word?  Maybe you are reviewing a meeting agenda or reading a final draft for a new company policy. You can navigate to Outlook create a new email and copy paste the necessary text in.  This takes a bit of time and isn’t very efficient.  Instead try using our Word Macro Create Outlook Email with Selected Text!  This macro will generate an Outlook email directly out of Word pulling in any text you have selected into the Email Body.  This macro is a better way to generate a follow up email helping you get a quicker response. Instead of writing reminders on Post-it notes start using this macro Create Outlook Email with Selected Text today!

First time using VBA?

The Developer Tab is an additional section of the ribbon when activated allows you access to Visual Basic in Applications like Excel, Outlook, Word.
Setup Now
Once the Developer Tab is available you will have the capability to update your security to allow Macros to run in the current application.
Setup Now
A sub of code is a collection of objects and variables. For the code to successfully run a library of references needs to be set.
Setup Now

Looking for more?

Subscribe as an Insider to receive additional rights.

Contact Us

Looking to improve your computer processes?
Leverage & Lean is here to help!

Copyright © 2019 Leverage & Lean – Uptown Style WordPress theme by GoDaddy