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
'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 EmailCurrent As Outlook.MailItem '
Dim EmailTemplate As Outlook.MailItem '
Dim FolderPath As String '
Dim FolderTemp As Object '
Dim objAtt As Object '
Dim objFSO As Object '
Dim oFile As Object '
Dim oFolder As Object '
Dim oFSO As Object '
Dim strPath As String '
Dim strFile As String '
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 oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.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
If EmailTemplate.Attachments.Count > 0 Then 'If the Email Template has Attachments
Const olByValue = 1
Const olEmbeddeditem = 5
Const TemporaryFolder = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set FolderTemp = objFSO.GetSpecialFolder(TemporaryFolder)
strPath = FolderTemp.Path & "\"
For Each objAtt In EmailTemplate.Attachments 'For each Email Template Attachment add to Current Email
Select Case objAtt.Type
Case olByValue, olEmbeddeditem
strFile = strPath & objAtt.Filename
objAtt.SaveAsFile strFile 'Save the Attachment to the Temporary Folder
End Select
If InStr(1, EmailTemplate.HTMLBody, "cid:image") > 0 And InStr(1, EmailTemplate.HTMLBody, ".png") > 0 And InStr(1, objAtt.Filename, ".jpg") = 0 Then
EmailCurrent.Attachments.Add strFile, olByValue 'If there are embedded png images in the selected Email Template exclude jpg attachments
ElseIf InStr(1, EmailTemplate.HTMLBody, "cid:image") = 0 And InStr(1, EmailTemplate.HTMLBody, ".png") = 0 Then
EmailCurrent.Attachments.Add strFile, olByValue 'If no embedded png images in the selected Email Template attach everything
End If
objFSO.DeleteFile strFile 'Delete the Attachment in the Temporary Folder
Next
End If
'EmailCurrent.Subject = EmailTemplate.Subject 'Set Active Email's Subject
EmailCurrent.HTMLBody = EmailTemplate.HTMLBody & CurrentBody 'Set Active Email's Body
EmailCurrent.Display
'EmailCurrent.Send

Set EmailCurrent = Nothing
Set EmailTemplate = Nothing
Set FolderTemp = Nothing
Set objAtt = Nothing
Set objFSO = Nothing
Set oFile = Nothing
Set oFolder = Nothing
Set oFSO = 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

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.
30. ‘Set EmailCurrent = EmailCurrent.Reply ‘If you want to Reply with an Email Template This is the code line to update if you want to only Reply to the Sender and not all email recipients
31. Set EmailCurrent = EmailCurrent.ReplyAll ‘If you want to ReplyAll with an Email Template By Default the selected email will Reply to all email recipients. Remove this code line and update code line #30 to only reply to sender
37. FolderPath = “C:\Users\” & User & “\AppData\Roaming\Microsoft\Templates\” ‘Default Outlook Email Template path. You can update the FolderPath to somewhere else. The Folder Path will look to Outlook’s default file path where email templates are saved. You can update this to a different path if you save your email templates elsewhere
86. If InStr(1, EmailTemplate.HTMLBody, “cid:image”) > 0 And InStr(1, EmailTemplate.HTMLBody, “.png”) > 0 And InStr(1, objAtt.Filename, “.jpg”) = 0 Then If there are embedded png images in the selected Email Template exclude jpg attachments If the selected email template has images in the email body VBA will attempt to attach jpg files of the same images. When this happens, it looks odd to see the same images attached are also in the email body. This code line ensures that these jpg images are not attached in the email response
88. ElseIf InStr(1, EmailTemplate.HTMLBody, “cid:image”) = 0 And InStr(1, EmailTemplate.HTMLBody, “.png”) = 0 Then If no embedded png images in the selected Email Template attach everything If the selected email template does NOT have images in the email body then attach all attachments found
94. ‘EmailCurrent.Subject = EmailTemplate.Subject ‘Set Subject If you want to replace the active email’s subject with the selected email template’s subject
95. EmailCurrent.HTMLBody = EmailTemplate.HTMLBody & CurrentBody ‘Set Body Set the active email body with the email template’s contents and past email body content
97. ‘EmailCurrent.Send By Default the email will display for review before sending. If you are confident that the email can send immediately after finishing this macro update this code line
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments

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