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 '//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 'https://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
Simple Reply, Reply All, Forward
When your are using the same code over and over in separate macros, such as in this case where the code is identical except for the single line that determines whether it is a reply, reply all, or forward, you can use a simple macro to pass values to the main macro. This keeps the module smaller, makes it easier to read and edit as you only need to make a change once.
In this example, Dim oResponse was moved outside of the macros so it applies globally. The Reply, ReplyAll, and Forward macros are used to set the type of response, then calls the main macro to complete the action.
Option Explicit Private WithEvents oExpl As Explorer Private WithEvents oItem As MailItem Private bDiscardEvents As Boolean Dim oResponse As MailItem 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 Set oResponse = oItem.Reply afterReply End Sub Private Sub oItem_Forward(ByVal Response As Object, Cancel As Boolean) Cancel = True bDiscardEvents = True Set oResponse = oItem.Forward afterReply End Sub Private Sub oItem_ReplyAll(ByVal Response As Object, Cancel As Boolean) Cancel = True bDiscardEvents = True Set oResponse = oItem.ReplyAll afterReply End Sub Private Sub afterReply() oResponse.Display ' do whatever here End Sub
For example to mark or clear flagged messages complete when replying, use this code for the afterReply sub, either marking the flag complete or clearing the flag completely.
Private Sub afterReply() oResponse.Display If oItem.IsMarkedAsTask Then oItem.MarkAsTask olMarkComplete 'oItem.ClearTaskFlag oItem.Save End If End Sub
To change the From address to an lias or shared mailbox, use this afterReply macro.
Note that when you set the From address, you need to change it before you display the message.
Private Sub afterReply() ' do whatever here oResponse.SentOnBehalfOfName = "olsales@domain.com" oResponse.Display End Sub
Add display names to the message
This version of the afterReply macro (which works with the Simple Reply macro, above) adds the recipient's names to the top of the reply. If only a recipient's email address in the To or CC field, the address is entered.
You could use Instr function to grab the alias from the address (the part before the @) and could use Instr to get the name before the first space, provided all names are in 'first last' format. When recipient's use a mix of 'first last' and 'last, first' format, you would need to also check for a comma and grab the next word.
Private Sub afterReply() oResponse.Display ' get the recipient names Dim Recipients As Outlook.Recipients Dim R As Outlook.Recipient Dim i Dim strTo As String, strCC As String Set Recipients = oResponse.Recipients ' step backwards so the order of the names is ' the same as in the address fields For i = Recipients.Count To 1 Step -1 Set R = Recipients.Item(i) If R.Type = olCC Then strCC = R.Name & ", " & strCC Else strTo = R.Name & ", " & strTo End If Next 'insert the names Dim olInspector As Outlook.Inspector Dim olDocument As Word.Document Dim olSelection As Word.Selection Set olInspector = Application.ActiveInspector() Set olDocument = olInspector.WordEditor Set olSelection = olDocument.Application.Selection olSelection.InsertBefore "CC: " & strCC olSelection.InsertParagraphBefore olSelection.InsertBefore "To: " & strTo End Sub
How to use this macro
This macro "watches" for you to click the Reply or Reply All button and "does something".
First: You will need macro security set to low .
To check your macro security in Outlook 2010 and above, 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.
Open the VBA Editor by pressing Alt+F11 on your keyboard.
To use the macro code in ThisOutlookSession:
- 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.)
- If the macro uses Word objects, you'll need to set Word as a Reference in the VBA Editors Tools > References dialog.
/a>
More information as well as screenshots are at How to use the VBA Editor
To test this macro, click in Application_Startup and press Run. Open a message that has one or more attachments and click Reply.
Hi Diane.
I have an Outlook macro that I want to only run for new e-mails, not replies or forwards. I think the general principles here would apply to new e-mails, but not sure how to 'watch' or check if the e-mail is a new one. Thank you.
Hi Diane,
I Need your help to code " reply with attachment and body emails"
Hello Diane. I am exploring the feasibility to allow a recipient ok an email to respond using dropdown menus. I found many ideas related to incorporating VBA in sender mails but none in recipient. The case is: I am sending an email to you and you reply to me via this email with specific filled boxes I will use later to automate reception of mail. Also, would like to allow the recipient to enclose a file and prevent sending if the file is not enclosed. Thank you 1000X Yvan
That is not possible in Outlook, for security reasons.
This is such helpful code and examples. Unfortunately, I am completely baffled as to how to get the oItem_Reply event to trigger (in my code below, the name is different, but I've tried it with exactly the same names as your code as well with no luck). I have other events that trigger correctly (e.g., Application_ItemSend), but nothing I try will get oItem_Reply to trigger. Things I've tried: Confirmed that I am not using Outlook 2013's reading pane reply--Since I am using Outlook 2016, this should not be the problem and I am not clicking reply in the reading pane. My replies always open in a new popup window. I have confirmed that all the code is in the ThisOutlookSession module. I have exited Outlook and restarted. I have rebooted the computer. I cannot fathom what else I can try. Here are relevant code snippets: Public WithEvents objExplorer As Outlook.Explorer Public WithEvents objInspectors As Outlook.Inspectors Public WithEvents objMail As Outlook.MailItem Private bDiscardEvents As Boolean Dim oResponse As MailItem Private Sub Application_Startup() Set objExplorer = Outlook.Application.ActiveExplorer bDiscardEvents = False End Sub Private Sub objExplorer_SelectionChange() On Error Resume Next Set objMail = objExplorer.Selection.Item(1) End Sub Private Sub objMail_Reply(ByVal Response As Object, Cancel… Read more »
You have the format property wrong. olFormatHTML not olHTMLformat
This snippet is from Always Reply Using HTML Format in Outlook (slipstick.com)
Set Response = objmail.Reply
Response.BodyFormat = olFormatHTML
Response.Display
Yes, true enough--I have fixed the olFormatHTML issue. Thank you.
But fixing that doesn't solve the real problem I'm having which is that the code never runs, so even after correcting that, the event is never triggered.
Actually, that's not quite true: The really odd thing is that randomly, the code will run! It might run 2 or 3 times and then stop. Or, it might not run at all for days, but then when I restart Outlook a couple days later, it will run again a few times (I think 6 times is the most it has run in a row) and then stop running for subsequent Replies. I am completely baffled.
Any additional thoughts? Thanks for taking the time to ponder this.
ifi t only runs when you restart outlook, there is something causing the autostart macro to fail.
Add this macro to ThisOutlookSession then add a button for it on the ribbon or quick access toolbar - then run it when the macro isn't working. Does the macro work again?
Sub RunStart()
Call Application_Startup
MsgBox "App Start Started"
End Sub
That's the thing: I am confident that he autostart macro is running because Outlook pops up the warning asking me if I want to enable macros when I start Outlook. Again, oddly, sometimes Outlook asks me if I want to enable macros when Outlook is loading up, but other times I am not asked until I run the first macro of the session using a Quick Access button. At any rate, I did add the RunStart macro to ThisOutlookSession and added a button on the Quick Access toolbar. I verified that the objMail_Reply macro was not getting triggered by clicking the reply button on the Ribbon and the code did not stop in objMail_Reply so that confirmed it was not triggered. I then clicked the button to run the RunStart macro and confirmed that it had run because I got the popup message stating "App Start Started." Unfortunately, when I then clicked the reply button on the Ribbon, the objMail_Reply code did not run. It is so bizarre that sometimes it works and sometimes it does not. I really wish I could figure out the pattern for what causes the difference in behavior. I'm open to any other things to… Read more »
Hi Ron,
Did you ever get this working?
No, I did not.
Diane can you please help me? I have been trying to change the “From” field after a user hits “Reply”. But the “From” field only needs to be updated for emails with a subject line that has the text “MQ ID:” in it.
I noticed that it says the “From” field needs to be changed before the item is displayed but I don’t get how to do that in This Outlook Session.
Could you please show me how to do this?
Thanks,
Mark
It needs changed before .display.
Private Sub afterReply()
' change sender here
Dim olNS As Outlook.NameSpace
'use first account in list
oResponse.SendUsingAccount = olNS.Accounts.Item(1)
oResponse.Display
' get the recipient names
Dim Recipients As Outlook.Recipients
Dim R As Outlook.Recipient
Dim i
Dim strTo As String, strCC As String
Macros to send messages using a specific account (slipstick.com)
Hi, This is all very interesting and quite new to me. My little experience in Macros is with the macro records which makes life very easy. I have my desktop outlook account, liked to a gmail account which is itself link to a work account I have. I know this linking my seem bizarre but it works really well for me actually with one single exception: When I answer an email of that work account from my outlook, it automatically sent my gmail email as the sender. At the moment, every time I write an email I have to remember to manualy change the sender address to my work address which is rather anoying. I've been going through your Macro post trying to find a way so that every time I create a new email or answer or forward one, the macro automatically changes the sender address to my work email address instead of the gmail. I'm pretty sure this is possible right? Following some of the examples on this page I managed to build a macro that created a new blank email with the correct sender email address filled out but I can't manage to set this up so… Read more »
This should help -
https://www.slipstick.com/outlook/using-gmail-master-account-reply-using-correct-account/
Is the work account configured in Outlook? If you always want to use it, you can change it in the account settings.
https://www.slipstick.com/outlook/using-gmail-master-account-reply-using-correct-account/
Thanks Diane! I think the first link might have what I need. :)
I'll go through it now.
best
The script has a side effect when browsing Outbox. Any email selected (including the one active when selecting the Outbox folder) will loose it's sent info, and will hang in the Outbox. All my emails are sent after a 60 s delay, and browsing or even modifying and re-sending in the Outbox folder does not work, and messages will get stuck in Outbox.
As a workaround, I disabled the selection of items when in the Outbox (Explorer.CurrentFolder = "Outbox").
I never look in the outbox, so never noticed it, but knowing how the outbox works, it makes sense.
Several addins will 'mark as read' the selected messages if you view the outbox.- to send, you need to open the message, change folders then hit Send then stay out of the outbox until the message is sent.
https://www.slipstick.com/problems/after-viewing-outlooks-outbox-the-messages-in-it-wont-send/
What is the funtion of the the bDiscardEvents and, if there is a purpose for it, should it be set to false in the afterReply?
In this version of the macro, it looks like its not doing anything... in the original maco, it was used much like cancel. When the macro was edited for this example, it wasn't removed. :(