• Outlook User
  • Exchange Admin
  • Office 365
  • Outlook Developer
  • Outlook.com
  • Outlook Mac
  • Outlook & iCloud
    • Common Problems
    • Outlook BCM
    • Utilities & Addins

Run a script rule: Autoreply using a template

Slipstick Systems

› Developer › Run a script rule: Autoreply using a template

Last reviewed on August 24, 2020     103 Comments

A security update disabled the Run a script option in the rules wizard in Outlook 2010 and all newer Outlook versions. See Run-a-Script Rules Missing in Outlook for more information and the registry key to fix restore it.

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.

Sub AutoReplywithTemplate(Item As Outlook.MailItem)
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:\path\to\template.oft")

With oRespond 
    .Recipients.Add Item.SenderEmailAddress
    .Subject = "Your Subject Goes Here"
    .HTMLBody = "Your reply text goes here." & vbCrLf & _
              "---- 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

 

Autoreply to Reply To Address, Not Sender

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

Set oRespond = Item.Reply

' 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
    Dim strAddress As String

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
           strAddress = M.SubMatches(1)
        Next
    End If

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

 objMsg.Recipients.Add strAddress

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

' use for testing 
 objMsg.display 

 ' objMsg.Send

 End Sub

 

Reply with a template

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 fromhttp://slipstick.me/e8mio
Set Item = GetCurrentItem()

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

With oRespond
    .Recipients.Add Item.SenderEmailAddress
    .subject = "Your Subject Goes Here"
    .HTMLBody = oRespond.HTMLBody & vbCrLf & _
              "---- original message below ---" & vbCrLf & _
               Item.HTMLBody & vbCrLf
               
' 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

Testing a Run a Script macro

To test the macros without sending yourself messages, select a message and run this macro. Don't forget to change YourMacroName to the macro name first.

Sub RunScript()
Dim objApp As Outlook.Application
Dim objItem As Object ' MailItem
Set objApp = Application
Set objItem = objApp.ActiveExplorer.Selection.Item(1)

'macro name you want to run goes here
YourMacroName objItem

End Sub

More Information

More Run a Script Samples:

  • Autoaccept a Meeting Request using Rules
  • Automatically Add a Category to Accepted Meetings
  • Blocking Mail From New Top-Level Domains
  • Convert RTF Messages to Plain Text Format
  • Create a rule to delete mail after a number of days
  • Create a Task from an Email using a Rule
  • Create an Outlook Appointment from a Message
  • Create Appointment From Email Automatically
  • Delegates, Meeting Requests, and Rules
  • Delete attachments from messages
  • Forward meeting details to another address
  • How to Change the Font used for Outlook's RSS Feeds
  • How to Process Mail After Business Hours
  • Keep Canceled Meetings on Outlook's Calendar
  • Macro to Print Outlook email attachments as they arrive
  • Move messages CC'd to an address
  • Open All Hyperlinks in an Outlook Email Message
  • Outlook AutoReplies: One Script, Many Responses
  • Outlook's Rules and Alerts: Run a Script
  • Process messages received on a day of the week
  • Read Outlook Messages using Plain Text
  • Receive a Reminder When a Message Doesn't Arrive?
  • Run a script rule: Autoreply using a template
  • Run a script rule: Reply to a message
  • Run a Script Rule: Send a New Message when a Message Arrives
  • Run Rules Now using a Macro
  • Run-a-Script Rules Missing in Outlook
  • Save all incoming messages to the hard drive
  • Save and Rename Outlook Email Attachments
  • Save Attachments to the Hard Drive
  • Save Outlook Email as a PDF
  • Sort messages by Sender domain
  • Talking Reminders
  • To create a rule with wildcards
  • Use a Macro to Copy Data in an Email to Excel
  • Use a Rule to delete older messages as new ones arrive
  • Use a run a script rule to mark messages read
  • Use VBA to move messages with attachments

Run a script rule: Autoreply using a template was last modified: August 24th, 2020 by Diane Poremsky
  • Twitter
  • Facebook
  • LinkedIn
  • Reddit
  • Print

Related Posts:

  • Send a New Message when a Message Arrives
  • A simple run a rule script marks Outlook messages read when the messag
    Use a run a script rule to mark messages read
  • Run a script rule: Reply to a message
  • Outlook AutoReplies: One Script, Many Responses

About 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.

Subscribe
Notify of
103 Comments
newest
oldest most voted
Inline Feedbacks
View all comments

Dan schwartz (@guest_214705)
February 4, 2020 11:53 am
#214705

fantastic solutions. All I need now is specifying the email account to auto respond to?

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Dan schwartz
February 7, 2020 11:22 pm
#214727

Do you mean the which of your accounts in Outlook or which sender? Either can be controlled by a conditions in a rule.

0
0
Reply
alex (@guest_214206)
October 31, 2019 6:00 am
#214206

Hey. How to make an auto reply to an email in the message body.
need to send to the mail where the field: EMAIL:

From: no-reply@test.com
Sent: Friday, September 13, 2009 12:11 PM
To: HR
Subject: summary job

Name: Alex
Телефон: 88002000600
Email: global@mail.com "they are always different"

0
0
Reply
Sripathi (@guest_213557)
July 6, 2019 11:48 am
#213557

Hi,
Is it possible to do auto forward a mail when outlook is off (system is shutdown) based on some conditions? Not all the mails.
Thanks

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Sripathi
July 11, 2019 10:24 pm
#213594

Only if you use an Exchange mailbox and can set up a server side rule to handle it.

0
0
Reply
Vishal (@guest_210022)
January 19, 2018 6:44 am
#210022

Hi, I am trying to retrieve "date" from the Subject Line.
For Ex : if the subject line reads like the one in below :

PPT | Planned Maintenance Work | Check | 22:00 - 23:59 Jan 24, 2018 GMT+7 || Viao

Firstly, want to retrieve the date from the subject line and then, I want to check if the date "Jan 24,2018" is less than 10 days from Today.
If yes, I want to trigger a template autoreply. Is this a feasible task ?

Looking out for the leads.

Thanks !

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Vishal
January 19, 2018 11:23 am
#210026

it is feasible. Will they all begin with ppt or be from the same sender - some way to filter them out from other messages?
using the macro at https://www.slipstick.com/developer/code-samples/create-appointment-email-automatically/ as an example,
If InStr(1, LCase(Item.Subject), "ppt") Then
Dim apptArray() As String
'split the subject
apptArray() = Split(Item.Subject, "|")
use the values:
strDate = apptArray(4) ' it might be 3
you'll need to convert it to a date and test it. I don't have the correct code to convert "22:00 - 23:59 Jan 24, 2018 GMT+7" to a date committed to memory (or published here) though. Even better is if the date and time have a separator, so they are split into separate fields.

The other option is using regex instead of an array - it would look for the date format and extract it. That actually might be better, especially if the month is always 3 letters - but if you need to use values from the subject, the array grabs them for you. A macro example using regex is here: https://www.slipstick.com/developer/regex-parse-message-text/

1
-1
Reply
Vishal (@guest_210072)
Reply to  Diane Poremsky
January 22, 2018 2:21 am
#210072

Diane, thanks for the kind reply.
Yes, "ppt" is common in the subject line. However, the date "Jan 24,2018" will be appearing in the text format like the one stated above after the time "22:00-23:59". How do I make sure I run a formula of finding the date difference between system date .i.e today with the date in the subject line and then if it is less than 10 days, make a rule to move it to a specified folder "<10days".

0
-1
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Vishal
February 13, 2018 8:54 am
#210330

As long as the date format is the same, regex can find it. you'd use a pattern something like ((\w)[3]\s(\d)[1,2],\s(\d)[4] VBA can convert it to a date value, which is subtracted from today's date.
You'd use this code or similar: https://www.slipstick.com/developer/run-a-script-rule-autoreply-using-a-template/#webform

Although used for other purposes, there are regex samples at https://www.slipstick.com/developer/regex-parse-message-text/

(I'm not good at writing regex off the top of my head, so it might not be exactly correct, but will be similar.)

0
-1
Reply
Aaron Rizo (@guest_209310)
November 15, 2017 4:27 pm
#209310

Hello all!

I need a bit of help with a script from this page. Essentially, I'm trying to set up a script in Outlook that will take an email that I'm receiving from a "No Reply" and use the email address in the body to reply to it instead with a template. The script I'm using is from the "Reply to a web form generated message" and I keep getting an error when it's running that states "Object required: Error 424." Any help would be much appreciated!

0
-1
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Aaron Rizo
November 16, 2017 12:59 pm
#209332

Do you know which line it dies on? Are you properly referring to the reply message?

Set oRespond = Item.Reply
oRespond.to strAddress 'replaces the reply address with strAddress value

0
-1
Reply
Ashish (@guest_207695)
July 14, 2017 8:40 am
#207695

Hi Diane, I am trying out your code for 'Reply to a web form generated message' and it does not work for me. I changed the .Display to .Send but nothing happens. I tried to run the rule manually several times & upon receiving a new message but the AutoSend does not send any reply. Can you please confirm anything to be configured in addition to the script? Thank you.

0
-1
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Ashish
July 14, 2017 11:31 pm
#207703

Does it display the message all properly filled out and formatted, when you have it set to .display?

0
-1
Reply
Channing Workman (@guest_206995)
June 6, 2017 12:30 pm
#206995

Diane,

Thank you for your excellent example! It has been very helpful to me.

I'm struggling a tad on a regex expression.

I would like to extract an email address from the Emails body

--Formatted as such--
Email Address: test@gmail.com

The email address is a blue link when I open the email and I'm not sure that may have something to do with it.

This is my expression:

.Pattern = "(Email Address[:]([w-s]*)s*)n"
.Global = False

Any thoughts?

Thank you!

Channing

0
-1
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Channing Workman
July 14, 2017 11:37 pm
#207704

Sorry i missed this, i thought I replied before. :( As long as the address is on one line, you can use

"(Email Address[:]\s*(.*))\n?"

I've also used ((.*)@(.*)) for the address.

0
-1
Reply
Ernst (@guest_203145)
November 29, 2016 4:54 am
#203145

Hi Diane,
I am trying to use your "Autoreply to Reply To Address, Not Sender" script to create a rule sending a server-side response to people who've filled a form on my website. Outlook apparently needs to be running for this rule to function, do you confirm? Is there a workaround? I am setting this rule on a noreply address which I do not open via Outlook.
Thanks for your support.
Ernst

1
-1
Reply

Visit Slipstick Forums.
What's New at Slipstick.com

Latest EMO: Vol. 28 Issue 11

Support Services

Do you need help setting up Outlook, moving your email to a new computer, migrating or configuring Office 365, or just need some one-on-one assistance?

Subscribe to Exchange Messaging Outlook






Our Sponsors

CompanionLink
ReliefJet
  • Popular
  • Latest
  • WeekMonthAll
  • Adjusting Outlook's Zoom Setting in Email
  • How to Remove the Primary Account from Outlook
  • Cannot add Recipients in To, CC, BCC fields on MacOS
  • Move an Outlook Personal Folders .pst File
  • Save Sent Items in Shared Mailbox Sent Items folder
  • Create rules that apply to an entire domain
  • Outlook's Left Navigation Bar
  • Remove a password from an Outlook *.pst File
  • Use PowerShell to get a list of Distribution Group members
  • View Shared Calendar Category Colors
  • Cannot add Recipients in To, CC, BCC fields on MacOS
  • Change Appointment Reminder Sounds
  • Messages appear duplicated in message list
  • Reset the New Outlook Profile
  • Delete Old Calendar Events using VBA
  • Use PowerShell or VBA to get Outlook folder creation date
  • Outlook's Left Navigation Bar
  • Contact's Display Bug
  • Use PowerShell to get a list of Distribution Group members
  • Edit Outlook’s Attach File list
Ajax spinner

Newest Code Samples

Delete Old Calendar Events using VBA

Use PowerShell or VBA to get Outlook folder creation date

Rename Outlook Attachments

Format Images in Outlook Email

Set Outlook Online or Offline using VBScript or PowerShell

List snoozed reminders and snooze-times

Search your Contacts using PowerShell

Filter mail when you are not the only recipient

Add Contact Information to a Task

Process Mail that was Auto Forwarded by a Rule

Recent Bugs List

Microsoft keeps a running list of issues affecting recently released updates at Fixes or workarounds for recent issues in Outlook for Windows.

Outlook for Mac Recent issues: Fixes or workarounds for recent issues in Outlook for Mac

Office Update History

Update history for supported Office versions is at Update history for Office

Outlook Suggestions and Feedback

Outlook Feedback covers Outlook as an email client, including Outlook Android, iOS, Mac, and Windows clients, as well as the browser extension (PWA) and Outlook on the web.

Use Outlook.com Feedback for suggestions or feedback about Outlook.com accounts.

Other Microsoft 365 applications and services




Windows 10 Issues

  • iCloud, Outlook 2016, and Windows 10
  • Outlook Links Won’t Open In Windows 10
  • Outlook can’t send mail in Windows 10: error Ox800CCC13
  • Missing Outlook data files after upgrading Windows?

Outlook Top Issues

  • The Windows Store Outlook App
  • The Signature or Stationery and Fonts button doesn’t work
  • Outlook’s New Account Setup Wizard
  • Outlook 2016: No BCM
  • Exchange Account Set-up Missing in Outlook 2016

VBA Basics

How to use the VBA Editor

Work with open item or selected item

Working with All Items in a Folder or Selected Items

VBA and non-default Outlook Folders

Backup and save your Outlook VBA macros

Get text using Left, Right, Mid, Len, InStr

Using Arrays in Outlook macros

Use RegEx to extract message text

Paste clipboard contents

Windows Folder Picker

Custom Forms

Designing Microsoft Outlook Forms

Set a custom form as default

Developer Resources

Developer Resources

Developer Tools

Outlook-tips.net Samples

VBOffice.net samples

SlovakTech.com

Outlook MVP David Lee

MSDN Outlook Dev Forum

Repair PST

Convert an OST to PST

Repair damaged PST file

Repair large PST File

Remove password from PST

Merge Two Data Files

Sync & Share Outlook Data

  • Share Calendar & Contacts
  • Synchronize two computers
  • Sync Calendar and Contacts Using Outlook.com
  • Sync Outlook & Android Devices
  • Sync Google Calendar with Outlook
  • Access Folders in Other Users Mailboxes

Contact Tools

Data Entry and Updating

Duplicate Checkers

Phone Number Updates

Contact Management Tools

Diane Poremsky [Outlook MVP]

Make a donation

Calendar Tools

Schedule Management

Calendar Printing Tools

Calendar Reminder Tools

Calendar Dates & Data

Time and Billing Tools

Meeting Productivity Tools

Duplicate Remover Tools

Mail Tools

Sending and Retrieval Tools

Mass Mail Tools

Compose Tools

Duplicate Remover Tools

Mail Tools for Outlook

Online Services

Productivity

Productivity Tools

Automatic Message Processing Tools

Special Function Automatic Processing Tools

Housekeeping and Message Management

Task Tools

Project and Business Management Tools

Choosing the Folder to Save a Sent Message In

Run Rules on messages after reading

Help & Suggestions

Outlook Suggestion Box (UserVoice)

Slipstick Support Services

Home | Outlook User | Exchange Administrator | Office 365 | Outlook.com | Outlook Developer
Outlook for Mac | Common Problems | Utilities & Addins | Tutorials
Outlook & iCloud Issues | Outlook Apps
EMO Archives | About Slipstick | Advertise | Slipstick Forums
Submit New or Updated Outlook and Exchange Server Utilities

Send comments using our Feedback page
Copyright © 2023 Slipstick Systems. All rights reserved.
Slipstick Systems is not affiliated with Microsoft Corporation.

wpDiscuz

Sign up for Exchange Messaging Outlook

Our weekly Outlook & Exchange newsletter (bi-weekly during the summer)






Please note: If you subscribed to Exchange Messaging Outlook before August 2019, please re-subscribe.

Never see this message again.

You are going to send email to

Move Comment