# Run a script rule: Autoreply using a template

Last reviewed on August 13, 2014   —  22 comments

This macro will reply to the person who sent the message. It includes the original message, any text entered by in the macro, plus the content of the template.

Because this is a run a script rule, all messages meeting the condition will be replied to, unlike the autoreply rule which is one message per Outlook session.

If you want to include the original subject, use (or add) item.subject to the .Subject field, otherwise, this should have most of the bases covered and point you in the right direction for things I didn't include. Remove the parts you don't need and you should be good to go....

Keep in mind that the original message is Item, from Item As Outlook.MailItem. oRespond is the message you are sending back. You need to use the Item fields if you want to include fields from the original message in the response.


Dim oRespond As Outlook.MailItem

' Use this for a real reply

' This sends a response back using a template
Set oRespond = Application.CreateItemFromTemplate("C:\path\to\template.oft")

With oRespond
.Subject = "Your Subject Goes Here"
"---- original body below ---" & vbCrLf & _
Item.HTMLBody & vbCrLf & _
"---- Template body below ---" & _
vbCrLf & oRespond.HTMLBody

' includes the original message as an attachment

' use this for testing, change to .send once you have it working as desired
.Display
End With
Set oRespond = Nothing
End Sub



Reply to a web form generated message

If you want to reply to a person who fills out a web form but the email that is generated is from a generic email address, not their address, you can use RegEx to get the address.

Sub SendNew(Item As Outlook.MailItem)
Dim Reg1 As Object
Dim M1 As Object
Dim M As Object

Set Reg1 = CreateObject("VBScript.RegExp")

With Reg1
.Pattern = "(([\w-\.]*\@[\w-\.]*)\s*)"
.IgnoreCase = True
.Global = False
End With

If Reg1.Test(Item.Body) Then

Set M1 = Reg1.Execute(Item.Body)
For Each M In M1
Next
End If

Dim objMsg As MailItem
Set objMsg = Application.CreateItemFromTemplate("C:\path\to\template.oft")

' Copy the original message subject
objMsg.Subject = "Thanks: " & Item.Subject

' use for testing
objMsg.display

' objMsg.Send

End Sub


When you want to reply with a template, you need to "pick up" information from the selected message. This macro works with an open or selected item and needs the GetCurrentItem function found here. If you will only ever use it with selected items (or open items) you can change the code to use current item or selection. More information is available at Outlook VBA: work with open item or selected item.

Sub ReplywithTemplate()
Dim Item As Outlook.MailItem
Dim oRespond As Outlook.MailItem

' need function from http://slipstick.me/e8mio
Set Item = GetCurrentItem()

' This sends a response back using a template
Set oRespond = Application.CreateItemFromTemplate("C:\Test\template.oft")

With oRespond
.subject = "Your Subject Goes Here"
.HTMLBody = oRespond.HTMLBody & vbCrLf & _
"---- original message below ---" & vbCrLf & _
Item.HTMLBody & vbCrLf

' includes the original message as an attachment

' use this for testing, change to .send once you have it working as desired
.Display
End With
Set oRespond = Nothing
End Sub


#### Written by Diane Poremsky

A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.

Please post long or more complicated questions at Outlookforums.

### 22 responses to “Run a script rule: Autoreply using a template”

1. is there a way to to "reply all" to multiple messages at once?

2. can you use a script to send auto replies to people automatically if you have posted an item in your outlook calendar and marked it as out of office?

3. THANK YOU!!!!!!! This helps me so much! I have been trying to figure out how to do this for several months. Your helpful post has helped me accomplish something I have been working on for several months. Thank you so much for this helpful information!

4. I tried this for the "Out of Office" events but the script does not show up in the rules script selection window.

5. Rules and Alerts. I've also tried doing it without Rules and Alerts and just having the code in the ThisOutlookSession like you suggested above but that didn't work either.

6. This is the current code I'm trying without using rules. I was hoping it would work with a calendar event marked at "out of office".

Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
Dim Ns As Outlook.NameSpace
Set Ns = Application.GetNamespace("MAPI")
Set Items = Ns.GetDefaultFolder(olFolderCalendar).Items
End Sub
Private Sub Items_ItemAdd(ByVal Item As Object)
On Error Resume Next

If Item.Class = olAppointment And _
Item.FreeBusyStatus = "Out of Office" Then

Dim objMsg As MailItem
Set objMsg = Application.CreateItem(olMailItem)

objMsg.Subject = Item.Subject
objMsg.Body = "I am currently out of the office."

'use Display instead of Send if you want to add a note before sending
objMsg.Send

Set objMsg = Nothing

End If

End Sub

7. and I've tried this code with a script in the rules but it did not send auto replies when my calendar event showed me as out of office:

Dim oRespond As Outlook.MailItem
If Item.Class = olAppointment And _
Item.Category = "Out of Office" Then

oRespond
.HTMLBody = "I am currently out of the office" & vbCrLf & _
"---- original body below ---" & vbCrLf & _
Item.HTMLBody & vbCrLf & _
"---- Template body below ---" & _
vbCrLf & oRespond.HTMLBody

' use this for testing, change to .send once you have it working as desired
.Send
End If
Set oRespond = Nothing
End Sub

9. Thank you for your help! I'm not sure it is possible at this point. Good luck!

10. Yes, it seems that the rules to send an automatic email when I'm out can not be tied to a calendar event. I guess because the calendar is kind of separate from the mail part of Outlook and they do not communicate.

11. Thanks for the great post. I am using your auto-reply with some changes to support our needs. I have verified that the rule is running and at one point it told me a macro was going to run and I chose "enable", so I think it is trying to run. However, it never displays anything and if I try to put a break point in, it doesn't hit it. Here's the code:

**************
Dim Reg1 As Object
Dim M1 As Object
Dim M As Object

Set Reg1 = CreateObject("VBScript.RegExp")

With Reg1
.Pattern = "(([\w-\.]*\@[\w-\.]*)\s*)"
.IgnoreCase = True
.Global = False
End With

If Reg1.Test(Item.Body) Then

Set M1 = Reg1.Execute(Item.Body)
For Each M In M1
Next
End If

Dim objMsg As MailItem
Set objMsg = Application.CreateItem(olMailItem)

' Copy the original message subject
objMsg.Subject = "Some Subject"
objMsg.HTMLBody = "Some message"

' use for testing
objMsg.Display

'objMsg.Send

End Sub

**************
Thoughts?

12. Thanks. That did indeed take care of it.

13. I receive some 100 mails which i keep in a certain folder. I want to create a macro which will help me to send all the mails to the desired receipients (mails coming from the IDs) keeping a certain bunch of iDs in loop with a predefined templates. Pl help to resolve my query

If the Post Coment button disappears, press your Tab key.