I have a number of sample scripts for Run a Script rules, but not one that replies with a comment. Reply with a template? Sure thing. Change the subject and Forward the message? I've got that. Forward the message body to a new address or reply with a template? I have a script for that.
Now I have a script to automatically reply to a message that does not require a template.
This script requires Word as the email editor. It was tested in Outlook 2013 but should work in Outlook 2007 or 2010, and may work in older versions of Outlook as long as Word is the email editor. The myReply.Body method will work in all versions of Outlook, with both Word or the old Outlook editor.
This code uses Word VBA to insert the text without disturbing the body text. For this reason, it uses Application.ActiveInspector(), so we need to display the message then sending. It will flash on the screen for split second. If this is a problem, you can use the myReply.Body method (below)
Sub ReplywithNote(Item As Outlook.MailItem) Dim olInspector As Outlook.Inspector Dim olDocument As Word.Document Dim olSelection As Word.Selection Set myReply = Item.Reply myReply.Display Set olInspector = Application.ActiveInspector() Set olDocument = olInspector.WordEditor Set olSelection = olDocument.Application.Selection olSelection.InsertBefore "your text" ' uncomment to send ' myReply.Send End Sub
Item.Body method
To avoid the screen flash, use this code instead. This code checks the message format and if it's HTML, it uses the HTMLBody property. Plain text messages use the Body property.
Sub ReplytoMessage(Item As Outlook.MailItem) Set myReply = Item.Forward If Item.BodyFormat = olFormatHTML Then myReply.HTMLBody = "my comment" & vbCrLf & myReply.HTMLBody Else myReply.Body = "my comment" & vbCrLf & myReply.Body End If myReply.Display ' change to send after testing End Sub
Reply during certain time periods
This version sends replies if the message arrives before or after specified times. To reply during specified times, use And instead:
If Time > #8:00:00 AM# AND Time < #5:00:00 PM# Then
Note: Outlook needs to be running for Run a Script to work!
Sub AutoReplywithTemplate(Item As Outlook.MailItem) If Time < #8:00:00 AM# Or Time > #12:00:00 PM# Then Set myReply = Item.Reply myReply.HTMLBody = "my comment" & vbCrLf & myReply.HTMLBody ' Change display to send after testing myReply.Display End If End Sub
How to use Run a Script
To use this script, set macro security to Low, then open the VBA Editor and paste the code into ThisOutlookSession. Create a Run a Script rule, selecting this rule.
More Information
- 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
Hello
I have emails coming it that have a reply to email address in the properties. Is it possible with a macro to select that email without opening it up and have outlook reply to the email to the reply to email address and insert a template ?
I use outlook 2010 in win 10
Hi Diane,
Your website is great and has helped me many times. I am stuck now though and would appreciate your assistance greatly. I need to reply to several hundred messages so need automation. I want the reply message to include the original message and a few added sentences at the top. When I found this page I thought I had it made but I can't get any scripts to run. I have not scripted before in Outlook but have experience coding in other languages. I have the Macro security set to its lowest level, the MS Word Object Library referenced and tried in both Outlook 2013 and Outlook 2010 but nothing. Our exchange servers are Office 365. What should I check? Thanks much!!
Hi Diane,
Your code has helped me a lot, thank you very much for sharing/teaching.
Now, I have noticed all those emails that the script touched and replied are not marked as if they were, however, the send email resides in the sent items. I wonder if there is a way for the messages that got the automatic reply via rule-macro could be marked in the inbox (just as if you did reply manually), you know, with the purple arrow pointing left?
Thank you in advance for your advice and time.
Octaviano Gil
This is exactly what I'm trying to accomplish (reply to selected mail item with precanned text) but it's not working in Outlook 2007. After I create the macro it doesn't even show up in the Macros list unless I remove Item As Outlook.MailItem from Sub ReplytoMessage(Item As Outlook.MailItem) so it just reads Sub ReplytoMessage().
If I remove that then I get an error when running because I don't think myReply is declared. Tried declaring it as Dim myReply As Outlook.Mailitem but that doesn't work either.
Would appreciate some help! I'm very new to VBA scripting.
This part: Sub ReplytoMessage(Item As Outlook.MailItem) is used to make it work in a rule so the reply is automatic. It's not going to show up in the macros list as it can only be called by another macro.
To use it manually, you need to change the macro to work with the selected item or use a second macro. This macro picks up the selected item and passes it along to the replywithnote macro. To use only one macro, move item as out of the () and dim then set it, like i did, only using item instead of omail.
Sub testreplynote()
Dim oMail As Outlook.MailItem
Set oMail = ActiveExplorer.Selection.Item(1)
ReplywithNote oMail
End Sub
Thanks Diane - I was able to cobble together some other scripts and eventually got it working. I used the following.
Sub Approved()
Dim approvedReply As Outlook.MailItem
Dim approvedItem As Object
Set approvedItem = GetCurrentItem()
If Not approvedItem Is Nothing Then
Set approvedReply = approvedItem.ReplyAll
approvedReply.HTMLBody = "Approved" & vbCrLf & approvedReply.HTMLBody
approvedReply.Send
End If
Set approvedReply = Nothing
Set approvedItem = Nothing
End Sub
Function GetCurrentItem() As Object
Dim objApp As Outlook.Application
Set objApp = Application
On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
End Select
Set objApp = Nothing
End Function