Outlook Reply with Email Template | VBA Macro #32

Share on facebook
Share on twitter
Share on linkedin
If you find yourself emailing the same response over and over you could create an Email Template. If you have never done this checkout our tutorial on how to 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. By Default, the active email’s subject will remain but with an update to the VBA code you can replace it with the template’s subject. If the template you selected has attachments these will also be attached to the email. This Outlook macro is designed to grow with you over time and will display all of your email templates as you continue to create them. Checkout our other Outlook macros that makes it easier to save and open email templates.

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
***Recent Code Updates
08.09.2021 Better handling of embedded images in email template body.
09.06.2021 Exclude Embedded Images from also being attached to Email. Remove Extra Line Break before Email Signature. Close Email Template and Discard from Drafts Folder
10.06.2021 Update If oFile.Type = “Outlook Item Template Or Right(oFile.Name, 4) = “.oft” Then because macro won’t perform if run in a different language.
Copy the Code
'Leverage & Lean "Less Clicks, More Results"
Sub ReplywithEmailTemplate()
' Means variable is in use
Dim Counter As Integer '
Dim CurrentBody As String '
Dim DisplayFiles As String '
Dim Editor As Object '
Dim EmailCurrent As Outlook.MailItem '
Dim EmailTemplate As Outlook.MailItem '
Dim FolderPath As String '
Dim fso As Object '
Dim ImageCounter As Integer '
Dim objAttachment As Object '
Dim oBody As Word.Document '
Dim oFile As Object '
Dim oFolder As Object '
Dim Selection As Word.Selection '
Dim TempFolder As Object '
Dim TemplateName As String '
Dim TemplateNumber As String '
Dim User As String '
  
On Error GoTo LeverageLean
  
On Error Resume Next
Set EmailCurrent = Application.ActiveInspector.CurrentItem 'Select the Current Open Email
On Error GoTo 0
  
If EmailCurrent Is Nothing Then
Set EmailCurrent = Application.ActiveExplorer.Selection.Item(1) 'Select the Current Unopen Email
'Set EmailCurrent = EmailCurrent.Reply 'If you want to Reply with an Email Template
Set EmailCurrent = EmailCurrent.ReplyAll 'If you want to ReplyAll with an Email Template
End If
  
CurrentBody = EmailCurrent.HTMLBody 'The Active Email's Body
  
User = (Environ$("Username"))
FolderPath = "C:\Users\" & User & "\AppData\Roaming\Microsoft\Templates\" 'Default Outlook Email Template path. You can update the FolderPath to somewhere else.
   
Set fso = CreateObject("Scripting.FileSystemObject") 'Set fso
Set oFolder = fso.GetFolder(FolderPath)
   
Counter = 1
    
For Each oFile In oFolder.Files 'Loop through every File in FolderPath
If oFile.Type = "Outlook Item Template" Then 'If the File is an .oft add to DisplayFiles
DisplayFiles = DisplayFiles & vbNewLine & Counter & ". " & Left(oFile.Name, Len(oFile.Name) - 4)
Counter = Counter + 1
End If
Next oFile
   
If Len(DisplayFiles) > 1023 Then 'If DisplayFiles exceeds the character limit display MsgBox
MsgBox ("You have exceeded the character limit of the InputBox. " & _
"Email templates that should be available for selection will not be able to display. " & _
"Try making the file names of your email templates shorter. " & _
"Navigate to your folder path here: " & FolderPath)
End If
   
TemplateNumber = InputBox(DisplayFiles, "Reply with Outlook Email Templates") 'Determine what Email Template to Reply with
If TemplateNumber = "" Then Exit Sub
  
Counter = 1
   
For Each oFile In oFolder.Files 'Loop through every File in FolderPath
If oFile.Type = "Outlook Item Template" Then 'If the File is an .oft
If Counter = TemplateNumber Then 'If the Counter is the same as the selected Template
TemplateName = oFile.Name 'Selected Email Template File Name
End If
Counter = Counter + 1
End If
Next oFile
  
Set EmailTemplate = Application.CreateItemFromTemplate(FolderPath & "\" & TemplateName) 'The Email Template selected
Set Editor = EmailTemplate.GetInspector.WordEditor
Editor.Content.Copy
 
'EmailCurrent.Subject = EmailTemplate.Subject 'Set Active Email's Subject
'EmailCurrent.HTMLBody = EmailTemplate.HTMLBody & CurrentBody 'Set Active Email's Body
If EmailTemplate.Attachments.Count > 0 Then 'If Attachments Exist on the Appointment
Set fso = CreateObject("Scripting.FileSystemObject") 'Set fso
Set TempFolder = fso.GetSpecialFolder(2)
ImageCounter = 1
For Each objAttachment In EmailTemplate.Attachments
If InStr(1, objAttachment.fileName, "image00" & ImageCounter) > 0 Or InStr(1, objAttachment.fileName, "image0" & ImageCounter) > 0 Then 'Exclude Embedded Images from also being attached to Email
ImageCounter = ImageCounter + 1
Else
objAttachment.SaveAsFile TempFolder.Path & "\" & objAttachment.fileName 'Save the Attachment to the Temporary Folder
EmailCurrent.Attachments.Add TempFolder.Path & "\" & objAttachment.fileName 'Add Attachment to Email
fso.DeleteFile TempFolder.Path & "\" & objAttachment.fileName 'Delete the Attachment in the Temporary Folder
End If
Next
End If
EmailCurrent.Display
Set oBody = Application.ActiveInspector.WordEditor
Set Selection = oBody.Application.Selection
Selection.Delete 'Remove Extra Line Break before Email Signature
Selection.Paste
'NewEmail.Send
EmailTemplate.Close olDiscard 'Close Email Template and Discard from Drafts Folder
  
Set Editor = Nothing
Set EmailCurrent = Nothing
Set EmailTemplate = Nothing
Set fso = Nothing
Set objAttachment = Nothing
Set oFile = Nothing
Set oFolder = Nothing
Set TempFolder = Nothing
  
Exit Sub
   
LeverageLean:
If Err.Number = 76 Then
EmailCurrent.Display
MsgBox ("Something went wrong. Ensure the Folder Path is correct. " & vbNewLine & "Don't hesitate to email me: brentschneider@leveragelean.com")
ElseIf TemplateNumber = 0 Or TemplateNumber > Counter - 1 Then
EmailCurrent.Display
MsgBox (Err.Number & " - " & Err.Description & vbNewLine & vbNewLine & "Don't hesitate to email me: brentschneider@leveragelean.com")
Exit Sub
End If
   
End Sub
'Stay Awesome

Insider Content

Here is the Insider code for this macro. This code will allow you to setup an Excel Workbook to better manage your Outlook Email Templates. Once the macro runs it will display each Excel Worksheet as a selection option. Once you select a Worksheet another message box will display the Outlook Email Template options you list in that Excel Worksheet. This is a great way to group similar templates into a worksheet for easier selection when replying to an email. 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, Microsoft Excel 16.0 Object Library

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 Access, Excel, Outlook, PowerPoint, 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
Subscribe as an Insider to receive additional rights.
If you like our content and want to show your support tip us here!

Contact Us

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

Leverage & Lean

Is focused on improving computer processes by Leveraging programming and applying Lean principles. It is our goal to make you more efficient and effective on your computer producing “Less Clicks and More Results!”