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
fantastic solutions. All I need now is specifying the email account to auto respond to?
Do you mean the which of your accounts in Outlook or which sender? Either can be controlled by a conditions in a rule.
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"
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
Only if you use an Exchange mailbox and can set up a server side rule to handle it.
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 !
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/
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".
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.)
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!
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
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.
Does it display the message all properly filled out and formatted, when you have it set to .display?
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
Sorry i missed this, i thought I replied before. :( As long as the address is on one line, you can use
I've also used ((.*)@(.*)) for the address.
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