Macro Intro
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.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***Recent Code Updates
07.26.2021 Updated ContactItem variable to Dim As Outlook.ContactItem. Removed olInspector variable. Help Outlook resolve recipients if code is running using Recipients.ResolveAll. |
09.12.2021 With an update to Office 365 SMTP Email Addresses were no longer being recognized in the code. Made a handful of updates to handle this going forward. Now looking for Recipient.Address containing “EXCHANGE ADMINISTRATIVE GROUP” to determine if SMTP PropertyAccessor needs to be used to evaluate actual Email Address. In Outlook Contact also looking to Email Display Name instead of Email Address when evaluating SMTP. This required an update to SetColumns to include Email1DisplayName. Thoughts were that Recipient.DisplayType = olRemoteUser would help but didn’t seem to resolve the issue so removing for now. |
03.29.2022 Changed ContactItem variable to an Object (instead of Outlook.ContactItem). To better determine SMTP looking for “EXCHANGE ADMINISTRATIVE GROUP” or “Exchange Administrative Group”. Ensured that the ContactItem.Class is olContact before looking for a the First Name. Distribution List or Contact Groups can cause issues if the Class isn’t looked for. When comparing email addresses to determine a match use Ucase to make both capitalized. |
'Leverage & Lean "Less Clicks, More Results" Sub HelloContactsFreeMacro() Const PR_SMTP_ADDRESS As String = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E" ' Means that these variables are in use Dim ContactEmailAddress As String ' Dim ContactItem As Object ' Dim ContactItems As Outlook.Items ' Dim ContactName As String ' Dim EmailCurrent As Outlook.MailItem ' Dim HelloContacts 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 SMTPCheck As Boolean ' Dim SMTP As Outlook.PropertyAccessor ' On Error GoTo LeverageLean Set olInspector = Application.ActiveInspector() Set RecipientEmail = olInspector.currentItem RecipientEmail.Recipients.ResolveAll 'Ensure Outlook has resolved all recipients before capturing RecipientEmailAddress 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 If InStr(1, Recipient.Address, "EXCHANGE ADMINISTRATIVE GROUP") > 0 Or InStr(1, Recipient.Address, "Exchange Administrative Group") > 0 Then SMTPCheck = True Set SMTP = Recipient.PropertyAccessor RecipientEmailAddress = SMTP.GetProperty(PR_SMTP_ADDRESS) 'Capture the first Recipient listed in the To field ElseIf InStr(1, Recipient.Address, "EXCHANGE ADMINISTRATIVE GROUP") = 0 And InStr(1, Recipient.Address, "Exchange Administrative Group") = 0 Then RecipientEmailAddress = Recipient.Address 'Capture the first Recipient listed in the To field End If 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, Email1DisplayName, 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 If ContactItem.Class = olContact Then If SMTPCheck = True Then ContactEmailAddress = ContactItem.Email1DisplayName 'Set the Contact's Email Address using Display Name to help with SMTP Address ElseIf SMTPCheck = False Then ContactEmailAddress = ContactItem.Email1Address 'Set the Contact's Email Address End If If InStr(1, UCase(ContactEmailAddress), UCase(RecipientEmailAddress)) > 0 Then ContactName = ContactItem.FirstName 'Set the ContactName with the Contact's First Name End If 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 & vbNewLine End If Set olDocument = Application.ActiveInspector.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 objApp = Nothing Set objNS = Nothing Set olDocument = Nothing Set olInspector = Nothing Set olSelection = Nothing Set SMTP = Nothing Exit Sub LeverageLean: MsgBox (Err.Number & " - " & Err.Description & vbNewLine & vbNewLine & "Don't hesitate to email me: brentschneider@leveragelean.com") End Sub 'Stay Awesome!
Insider Content
Here is the Insider code for this macro. In the Hello Contacts Free Macro if an Outlook Contact is not found the text “Hello,” displays without a First Name. The Hello Contacts Insider Macro will automatically create the Recipient as a NEW Contact to your Contacts Folder. 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 LibraryCustomization
These segments of code can be customized to personalize this macro. 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!
The SearchText Variable is used to determine where the macro should stop the selection of text to format. Update the text from “Stay Awesome,” to something that you always indicate at the end of your emails or text that starts your email signature. (Ex. “Stay Awesome,”; “Thank you,”; “Take care,”)