To do something automatically when you click the Reply button, you would use code similar to the following example, which adds a keyword to the subject.
To "do something" when you click Reply All, change the procedure name and one line in the code from Reply to ReplyAll. (Or copy the Reply code if you want to use both Reply and Reply All).
Private Sub oItem_ReplyAll(ByVal Response As Object, Cancel As Boolean)
Set oResponse = oItem.ReplyAll
To use the code when you click Forward, you'll change the lines to:
Private Sub oItem_Forward(ByVal Response As Object, Cancel As Boolean)
Set oResponse = oItem.Forward
This code won't work with Outlook 2013's "reading pane reply" feature.
Add keyword to subject
Use this code to change Outlook fields, such as adding a keyword to the subject, adding a recipient, a flag, or a color category.
Option Explicit Private WithEvents oExpl As Explorer Private WithEvents oItem As MailItem Private bDiscardEvents As Boolean 'http://slipstick.me/44b0w Private Sub Application_Startup() Set oExpl = Application.ActiveExplorer bDiscardEvents = False End Sub Private Sub oExpl_SelectionChange() On Error Resume Next Set oItem = oExpl.Selection.Item(1) End Sub ' Reply Private Sub oItem_Reply(ByVal Response As Object, Cancel As Boolean) Cancel = True bDiscardEvents = True Dim oResponse As MailItem Set oResponse = oItem.Reply ' add the fields here oResponse.Subject = "keyword " & oResponse.Subject oResponse.Display bDiscardEvents = False Set oItem = Nothing End Sub
Add boilerplate text to reply
To insert boilerplate text into the reply, replace the Reply code in the macro above with this code.
Note: to use Word VBA in Outlook (when Word is the email editor), you need to set a reference to the Word Object library in Tools, References.
' Reply 'http://slipstick.me/44b0w Private Sub oItem_Reply(ByVal Response As Object, Cancel As Boolean) Dim strText As String Dim olInspector As Outlook.Inspector Dim olDocument As Word.Document Dim olSelection As Word.Selection Cancel = True bDiscardEvents = True strText = "This is my note." Dim oResponse As MailItem Set oResponse = oItem.Reply oResponse.Display Set olInspector = Application.ActiveInspector() Set olDocument = olInspector.WordEditor Set olSelection = olDocument.Application.Selection olSelection.InsertBefore strText bDiscardEvents = False Set oItem = Nothing End Sub
How to use these macros
First: You need to have macro security set to low during testing. The macros will not work otherwise.
To check your macro security in Outlook 2010 or 2013, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, it’s at Tools, Macro Security.
After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.
You need to set a reference to Microsoft Word Object model in the VB Editor's Tools, References.
The macros on this page need to be in ThisOutlookSession. The instructions are below.
Open the VBA Editor by pressing Alt+F11 on your keyboard.
- Expand Project1 and double click on ThisOutlookSession.
- Copy then paste the macro into ThisOutlookSession. (Click within the code, Select All using Ctrl+A, Ctrl+C to copy, Ctrl+V to paste.)
More information as well as screenshots are at How to use the VBA Editor