Last reviewed on August 6, 2015   —  50 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



If you use a web form that comes from a generic address with a Reply to address set, you can use a simple macro to Reply to the message. Unlike the autoreply rule which replies to the sender, this reply is the same as hitting the Reply button yourself.

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

' use for testing
oRespond.Display
' oRespond.Send

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 and their address is not in the Reply to field, you can use RegEx to get the address from the message body.

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


1. Marlon says

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

• Diane Poremsky says

You can use a rule and Run Rules Now. I recommend moving the messages you want to reply to, to a new folder and running the rule on that folder. You could also do it using VBA but I don't think I have any code samples that do it, so you'd need to write your own or tweak one I do have.

2. jimdernie@gmail.com says

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. Avary says

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. Joseph says

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

• Diane Poremsky says

Are you in the Out of Office screen or Rules & Alerts? These scripts do not work in Out of Office settings for Exchange Server, only Rules & Alerts.

5. Joseph says

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. Joseph says

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

• Diane Poremsky says

Did you restart outlook or click in the Application_startup macro and press Run (F8) ? You also need macro security set to low but... this isn't going to work as it applies only when the appointment is added to the calendar. You want to check the calendar and send an OOF without setting enabling the OOF option.

You could use a rule, enabled by a macro that belongs to an appt marked OOF. Enable or disable an Outlook rule using reminders and VBA.

7. Joseph says

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

• Diane Poremsky says

This won't work for your purposes either. Sorry. I'll see what i can come up with that is better than using a reminder to turn a rule on.

8. Joseph says

9. Joseph says

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

• Diane Poremsky says

My initial thought is that it is not possible, but it will be interesting to look into doing it.

10. Joseph says

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.

• Diane Poremsky says

Yes, that is more or less correct.

11. Rod says

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?

• Diane Poremsky says

In Outlook 2010 or 2013, go to File, Options, Trust Center - Macro Settings and verify macros are enabled. (Tools, Trust center in Outlook 2007) I think the problem is the permissions - after its working and you no longer need to edit it, you can set sign the macro using selfcert and set the macro security higher.

12. Rod says

Thanks. That did indeed take care of it.

13. Tuhin Banerjee says

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

• Diane Poremsky says

So you want to reply with template1 if ID1 and use template2 if from ID2? That is possible. You need to use either an if statement or select case to identify the id and call up the right template.

14. kyle says

Is it possible to use the script at the top "Auto reply using a template" to only send the template the the person that is CC'd on the received email. In my case, I will get an autogenerated email from a system with the actual requester CC'd on the email, but I only need the template to be sent automatically to the CC'd individual and not the From address.

I figured the line below is what I need to modify, but I do not know VBA to know what the CC'd area is so I can modify it.

• Diane Poremsky says

You need to up the recipients collection - put this before the With oRespond (i put it before the Set orespond line) and use .Recipients.Add recip
Dim Recipients As Outlook.Recipients
Dim oRecip As Recipient
Dim recip As String

Set Recipients = Item.Recipients
For i = Recipients.Count To 1 Step -1
Set oRecip = Recipients(i)
If oRecip.Type = olCC Then
End If
Next

15. kyle says

Where would I put in the email address of the original sender in the code? I am also getting an error on the last line.
.Display End With Set oRespond = Nothing End Sub

Thanks for all the help.

• Diane Poremsky says

What error are you getting?

16. kyle says

Compile Error: Expected Expression.

Here is the code I have so far. Like I said, I have not done VBA, but I am trying to learn, so I probalby messed it up pretty good.

Dim oRespond As Outlook.MailItem
' Use this for a real reply
' Set oRespond = Item.Reply ' This sends a response back using a template
Set oRespond = Application.CreateItemFromTemplate("C:\Users\ksmith23\AppData\Roaming\Microsoft\Templates\*.oft")
Dim Recipients As Outlook.Recipients
Dim oRecip As Recipient
Dim recip As String

Set Recipients = Item.Recipients
For i = Recipients.Count To 1 Step -1
Set oRecip = Recipients(i)
If oRecip.Type = olCC Then
End If
Next

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 .Attachments.Add Item
' use this for testing, change to .send once you have it working as desired
.Display End With Set oRespond = Nothing End Sub

• Diane Poremsky says

It might be due to the missing line breaks here:.Display End With Set oRespond = Nothing End Sub
it should be 4 lines:
.Display
End With
Set oRespond = Nothing
End Sub

17. kyle says

That fixed the error. Thanks.

18. kyle says

I try to debug the code and it seems it is not working at the .Recipients.add recip

Here is what I have:

Dim oRespond As Outlook.MailItem
' Use this for a real reply
' This sends a response back using a template

Set oRespond = Application.CreateItemFromTemplate("C:\Users\ksmith23\Desktop\RequesterEmail.oft")
Dim Recipients As Outlook.Recipients
Dim oRecip As Recipient
Dim recip As String

Set Recipients = Item.Recipients

For i = Recipients.Count To 1 Step -1
Set oRecip = Recipients(i)
If oRecip.Type = olCC Then
End If
Next

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
.Send
End With
Set oRespond = Nothing
End Sub

• Diane Poremsky says

You need to replace Item.SenderEmailAddress with recip:
becomes

19. willkoper says

Is there an elegant way to test if the message is a reply to a message previously sent, e.g. to avoid an endless autoreply loop?

• Diane Poremsky says

Define "elegant". :) You could search the sent folder for a match... or if you can count on the autoreply's subject being tagged as an autoreply (or having specific text), you can check for that.

The subject test is easier...

Either of these snippets should work for search (they need some objects dim and set yet) - i don't know which is faster.

For Each objItem In SentFolder.Items
If InStr(1, objItem.subject, item.subject) Then
exit sub
End If
Next

or this format
strSearch = "[subject] = " & item.subject
Set objItem = colItems.Find(strSearch)

• willkoper says

Touché :)

I had originally been thinking to check for the sender's email in the body, e.g. "XXXX wrote:", but searching the subject is much easier (especially since I'm setting that anyway).

Just using If InStr ( item.Subject, "") = 0 to check the original message and reply if the phrase is not found.

Thanks for the script!

20. richnik says

Been trying the sample codes above to start me off, but whenevey I click on run, it asks me for the macro name. So I create one but it only has the

Sub fr()

End Sub

What am I missing here? Thanks.

• Diane Poremsky says

These macros run using rules or other automated methods, you don't run them manually. if you want to test an automatic macro, you can call it using another macro but need to set the item to run it on.

21. Nos says

1. I want to have an automated message on all mail received (via Outlook 2010).
2. Thank you Diane! Visiting this site helped so far.
3. I do not have VB programming knowledge - my query thus of "lower grade"...
4. I want (tried) the following (from your instructions given on top):

Dim oRespond As Outlook.MailItem
'Use this for a real reply

'This sends a response back using a template
Set oRespond = Application.CreateItemFromTemplate("C:\Program Files\Microsoft Office\Templates\1033\MAIL.oft")

With oRespond
.Subject = "RECEIPT CONFIRMATION"
.HTMLBody = "Thank you " & vbCrLf & _
Item.Subject & vbCrLf & _
". The matter raised will be attended to by officials relevant to this matter." & vbCrLf & _
"---- 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

5.1 When I want to run (F5) above, I'm asked to create a macro. Should I create a macro now?
5.2 The HTMLBody might not be correct. May you assist me?

• Diane Poremsky says

These macros are triggered by rules, not run manually.

What problem are you having with the html body format?

22. Diane Poremsky says

To manually run these macros on a selected message, use this code, replacing the macro-name as needed.

Public Sub RunManaully()
Dim objItem
Set objItem = Application.ActiveExplorer.Selection.Item(1)
End Sub

• Nos says

I want this to run automatically without a macro... (Thanks for above though.)

Where should I save/write the code:

Dim oRespond As Outlook.MailItem
'Use this for a real reply

'This sends a response back using a template
Set oRespond = Application.CreateItemFromTemplate("C:\Program Files\Microsoft Office\Templates\1033\MAIL.oft")

With oRespond
.Subject = "RECEIPT CONFIRMATION"
.HTMLBody = "Thank you " & vbCrLf & _
Item.Subject & vbCrLf & _
". The matter raised will be attended to by officials relevant to this matter." & vbCrLf & _
"---- 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

Steps I followed was:

2. I've created/set a rule for, after receiving a mail, send reply message using a saved template: at the moment just my e-mail signature. I wish to make this message more 'personal' and want to add the recipients name.

3. Then browsed for help and found your help: I learned that Outlook will only sent this reply once per session. Founding your code seemed beter than what Outlook offers.

4. I then opened Microsoft Outlook | Developer | Visual Basic and created a "module 1". (I wish I could sent a screenshot to you!) Is this the right place? A blank screen opened where I've written the code above...

5. I don't know programming. I "run" the program and it's asking to create a macro..? I don't want a macro. So, did I save this incorrectly?/Tried the coding at the incorrect place?

• Nos says

Hallo Diane. I still don't have this script running... (I wish I could enrol for a VBA programming course!)

I refer to your comment before mine above (March 27, 2015 at 10:05 am):

I came to realise that I don't know what a VBA macro is... (what it "looks" like) And therefor did not understand the said comment the way you might have liked me to understand it. You've provided code for manually checking the script. Unfortunately I don't even know whether that must be at the bottom of the script I already have or above!? I also don't know how to replace "the macro-name as needed"... I have then again tried to run the script in such a way to display: I've aked a colleague to send me mail, but I do not view (the reason for .display I guess) the proposed mail that will be sent to him... I've made the rule run from "run rules now" and an error message appeared:

RULES IN ERROR
Rule (name): where my name is in the To box
Error: The script "" doesn't exist or is invalid.

Which way will be better to help me: manually run the macro in order to preview the mail that will be sent (as written per script) or through .Display? Please help me...

• Diane Poremsky says

This error :
RULES IN ERROR
Rule (name): where my name is in the To box
Error: The script "" doesn't exist or is invalid.

Means it is looking for a script named something like this but is not finding it.

Rules only work with macros named in that format.

to manually check a script, put this after the macro and select a message then run the macro. The name of your macro goes in the script in place of AutoReplywithTemplate
Public Sub RunManaully()
Dim objItem
Set objItem = Application.ActiveExplorer.Selection.Item(1)
' this is the name of your macro
End Sub

23. Nand says

I am brand new to script writing and have absolutely no experience in writing any form of code. Please forgive my questions that are very basic.

1. Can you please tell me if the script written at the very top of the page under "reply with a template" will send a message using both the senders name and the actual email sent to. In other words, when I normally (manually) reply to an email in Outlook 2010 - the senders name and email address is in the To box. This makes it easier to find previous emails sent to an individual in the past. Can (should) -

be changed to add (or substitute) the senders name and email?

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

2. In the .subject = "Your Subject Goes Here" - I assume this can be changed to
.subject = item.subject

if one wants to keep the original subject line?

3. If one wants to keep the original message in the reply (and not as an attachment) then should the

line be removed from the code?

4. Does the following remain unchanged if the original email content is to be included at the bottom of the reply email (in order to be able to see the original email in the reply email)

.HTMLBody = oRespond.HTMLBody & vbCrLf & _
"---- original message below ---" & vbCrLf & _
Item.HTMLBody & vbCrLf

5. My ultimate aim is to be able to make a rule that includes this script and

a. manually run the rule on a folder with a collection of emails that need to be responded to using the SAME response template (I would like to be able to change and update the template.oft file from time to time - and I am aware how to do so. The template is in HTML format and I would want to maintain that). Otherwise everything I want to do can be otherwise done using a shortcut key with QuickSteps - which does not do HTML!!

b. The reply email will have the original senders name and email address in the To box - making it easier to find the previously emails to an individual

24. Jen Epstein says

Hi there, thanks for the VERY helpful post!

I've added the VBA code for "Reply to a web form generated message" and it works, but I have to manually send out this message.

Is there any way to have VBA code to auto-reply to a web form generated message?

So when I receive an email, the script would extract the email in the body and then auto-reply.

-Jen

• Diane Poremsky says

Change .Display to .Send - use .Display for testing so you can see it's working properly, then change it to .Send to fully automate it.

25. Ricky Gomez says

Hi I created a rule to run this code and it works except when including the original body (Item.HTMLBody) which is blank.

Is anybody else having this problem?

I googled it and it seems a lot of people are having this problem with no solution.

This happens if I create a rule and assign it this macro. The funny thing is that it works well if I run the rule manually once the item is in the inbox.

But when it runs automatically when an item arrives it does not work and always puts a bland body.

When I debug the code the body looks like this:

Some of the solutions online stated to add .Save before .Send but it doesn't fix it

• Diane Poremsky says

I'll do some testing and see if i can figure out why it might be failing. I know too much mail, too fast can cause rules to fail, including too many messages that use the script. Passing the work load off to a second rule often works. This example passes everything off but you could do some stuff in the first script before handing it off. It really depends on what all you are doing.

Sub SendNew(Item As Outlook.MailItem)
processmessage item
end sub

private sub processmessage(item as mailitem)
' do whatever
end sub

26. Scott says

Hi Diane,

I have been experiencing some trouble with all my scripts, none of them have sent even though I have included ".send" The parameters for the rule I am using are: Apply this rule after the message arrives, from invitations@linkedin.com, run Project1.AutoReplywithTemplate (my script) and mark it as read.
Any ideas of what to do? Any script I use won't send, no matter what. I'll keep trying different scripts, but as of now nothing works. Thanks in advance.

Scott

• Diane Poremsky says

Don't use the mark as read action - put item.unread = false in the code. I usually put it at the end so if the macro errors, it won't be marked as read. Actions in run a script rules often fail - everything needs to be in the script. It's usually rhe action that fails, so I don't think this is the causing it not to send.

Is it doing everything but sending? Does it open a message form if you change it to display?
Is macro security set to low?

27. Scott says

Also, I thought I should include the actual script:

Dim oRespond As Outlook.MailItem

' Use this for a real reply

' This sends a response back using a template
Set oRespond = Application.CreateItemFromTemplate("C:\users\snissenbaum\desktop\VC Response.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
.Send
End With
Set oRespond = Nothing
End Sub

Thanks again!

Scott

Please post long or more complicated questions at OutlookForums by Slipstick.com.

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