VBA Macro used in the Microsoft Office Outlook Application

Outlook Hello Contacts | VBA Macro #33

Share on facebook
Share on twitter
Share on linkedin
Starting an email with a Hello can look more personable with some VBA code! The Hello Contacts Outlook macro automates the entry of the text Hello followed by the First Name of the recipient you are emailing. This Outlook macro uses the recipient’s email address to loop through all the existing contact records for a matching email address. If a match is found the contact’s First Name field value is used in the beginning of the email. This macro can be fired from a custom button or trigger automatically when replying to an email. To utilize the Hello Contacts macro, you will need to add contacts in Outlook.  If you haven’t created an Outlook contact before checkout this link to see the multiple ways to do this. You can create a contact directly from an email message, with an import, or manually from scratch.

See it in Action!

Watch this video to see this macro in action.

The Code

Here 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, Microsoft Office 16.0 Object Library, Microsoft Word 16.0 Object Library
'Leverage & Lean "Less Clicks, More Results"
Sub HelloContacts()
' 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 HelloContacts As String '
Dim olDocument As Word.Document '
Dim olInspector As Outlook.Inspector '
Dim olSelection As Word.Selection '
Dim objApp As Application '
Dim objNS As NameSpace '
Dim Recipient As Recipient
Dim RecipientEmail As Object
Dim RecipientEmailAddress As String '

On Error GoTo LeverageLean

Set olInspector = Application.ActiveInspector()
Set RecipientEmail = olInspector.CurrentItem

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
HelloContacts = "Hello," & vbNewLine & vbNewLine
ElseIf ContactName <> "" Then 'If a Contact Name is found
HelloContacts = "Hello " & ContactName & "," & vbNewLine & vbNewLine
End If

Set olDocument = olInspector.WordEditor
Set olSelection = olDocument.Application.Selection
olSelection.TypeText HelloContacts 'Enter Hello Contacts at the beginning of the email

Set ContactItem = Nothing
Set ContactItems = Nothing
Set olDocument = Nothing
Set olInspector = Nothing
Set olSelection = Nothing
Set objApp = Nothing
Set objNS = Nothing

Exit Sub

LeverageLean:
MsgBox (Err.Number & " - " & Err.Description & vbNewLine & vbNewLine & "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

Although you can create a custom button to trigger the Hello Contacts macro on command you can also automate this macro with some additional VBA code.

Here is the additional code that can be 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 Hello Contacts 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 Greeting and Goodbye Contacts | VBA Macro #35

The Greeting and Goodbye Contact macro automates the entry of a Greeting “Good Morning” or “Hello” followed by the First Name of the recipient you are emailing. To identify the First Name this macro uses the recipient’s email address to loop through all the existing contact records for a matching email address. If a match is found the contact’s First Name field value is used in the beginning of the email. The appropriate Greeting and Goodbye is determined by the current time and the day of the week. The Greeting and Goodbye Contacts macro helps you deliver better customer service and create better connections with your clients!

VBA Macro used in the Microsoft Office Excel Application

Excel Custom Questionnaire to File | VBA Macro #34

This is an Excel macro that generates a custom questionnaire to complete.  The Role field is used to determine what questions display for specific users.  Once a user selects their Role, they can click Generate to see what questions they need to complete to finish the questionnaire.  After a user has completed all the required questions, they can click Finish to create a separate Excel Workbook with all the populated answers.   You can customize where this new file is saved making it easy to know when someone has completed a questionnaire and is awaiting review. There is also the option to send this separate Excel Workbook in an Outlook email with a couple updates to the VBA code. There is always a better way with VBA and this Excel macro helps standardize the collection of answers to specific questions. This can help lead to a more formal review process and eliminate mistakes.

VBA Macro used in the Microsoft Office Outlook Application

Outlook Reply with Email Template | VBA Macro #32

If you find yourself emailing the same response over and over you could create an Outlook Email Template. Email templates are a great way to save that perfectly drafted email and recall it again at a later date. One shortcoming in Outlook is that there isn’t an easy way to reply to an email directly with an email template. This is why we created the macro Reply with Email Template. This is an Outlook macro that will allow you to reply to an email with all the contents of a specific email template. You can run this macro for the current email selected in your inbox or from an open email. By Default, this macro will reply to all recipients. You can update the VBA code to only reply to the sender instead. Once running this Outlook macro will access your default email template file path and display an InputBox with all the available email templates to select from. Simply type the leading number of the email template you want to reply with and click Ok. This macro will finish by pulling in the selected email template’s contents to the active Outlook email body. If the template you selected has attachments these will also be attached to the email.

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