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
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 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 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 .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
- Convert RTF Messages to Plain Text Format
- Create a rule to delete mail after a number of days
- Forward meeting details to another address
- How to Change the Font used for Outlook's RSS Feeds
- Keep Canceled Meetings on Outlook's Calendar
- Move messages CC'd to an address
- 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
- 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
- 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
- Use VBA to move messages with attachments