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
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 Appointment From Email Automatically
- Delegates, Meeting Requests, and Rules
- 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
- Move messages CC'd to an address
- Open All Hyperlinks in an Outlook Email Message
- Outlook 2016 and Run-a-Script Rules
- 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: Change Subject then Forward Message
- 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
- Sort messages by Sender domain
- Use a Rule to delete older messages as new ones arrive
- Use a run a script rule to mark messages read