• Outlook User
  • New Outlook app
  • Outlook.com
  • Outlook Mac
  • Outlook & iCloud
  • Developer
  • Microsoft 365 Admin
    • Common Problems
    • Microsoft 365
    • Outlook BCM
    • Utilities & Addins

VBA Sample: Do Something When Reply is Clicked

Slipstick Systems

› Developer › VBA Sample: Do Something When Reply is Clicked

Last reviewed on January 15, 2025     33 Comments

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.

Use VBA to add a keyword to a subject field

Add a file number or keyword to the subject line of messages.

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).

Add a keyword to the subject of all messages sent.

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 the Outlook 2013 and newer "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.

Insert Text into a message when you click reply

Copy attachment names when replying

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 alias 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.

insert recipient names

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:

  1. Expand Project1 and double click on ThisOutlookSession.
  2. Copy then paste the macro into ThisOutlookSession. (Click within the code, Select All using Ctrl+A, Ctrl+C to copy, Ctrl+V to paste.)
  3. If the macro uses Word objects, you'll need to set Word as a Reference in the VBA Editors Tools > References dialog.
    Set a reference to Word/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.

VBA Sample: Do Something When Reply is Clicked was last modified: January 15th, 2025 by Diane Poremsky

Related Posts:

  • Macro to Reply, ReplyAll, or Forward and File
  • Copy attachment names when replying
  • Always Reply Using HTML Format in Outlook
  • VBA: Change the From Account on Replies

About Diane Poremsky

A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.

Subscribe
Notify of
33 Comments
newest
oldest most voted
Inline Feedbacks
View all comments

SJ MO2 (@guest_220052)
February 5, 2023 12:17 pm
#220052

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.

0
0
Reply
azman (@guest_219632)
August 15, 2022 1:31 am
#219632

Hi Diane,

I Need your help to code " reply with attachment and body emails"

0
0
Reply
Yvan (@guest_219167)
February 10, 2022 2:29 pm
#219167

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

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Yvan
July 20, 2022 12:55 am
#219558

That is not possible in Outlook, for security reasons.

0
0
Reply
Ron (@guest_218042)
April 28, 2021 11:25 am
#218042

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 »

Last edited 4 years ago by Diane Poremsky
0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Ron
April 29, 2021 4:51 pm
#218056

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 

1
0
Reply
Ron (@guest_218063)
Reply to  Diane Poremsky
April 29, 2021 6:21 pm
#218063

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.

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Ron
April 29, 2021 11:26 pm
#218064

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

0
0
Reply
Ron (@guest_218067)
Reply to  Diane Poremsky
April 30, 2021 9:39 am
#218067

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 »

0
0
Reply
Mervyn (@guest_219548)
Reply to  Ron
July 17, 2022 11:34 am
#219548

Hi Ron,

Did you ever get this working?

0
0
Reply
Ron (@guest_219553)
Reply to  Mervyn
July 19, 2022 8:12 am
#219553

No, I did not.

0
0
Reply
Mark (@guest_217893)
April 13, 2021 1:30 am
#217893

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

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Mark
April 29, 2021 4:32 pm
#218055

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)

0
0
Reply
Nicholas (@guest_215862)
September 14, 2020 1:03 am
#215862

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 »

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Nicholas
September 14, 2020 10:45 pm
#215865

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/

0
0
Reply
Nicholas (@guest_215868)
Reply to  Diane Poremsky
September 15, 2020 5:31 am
#215868

Thanks Diane! I think the first link might have what I need. :)
I'll go through it now.
best

0
0
Reply
Dan (@guest_213471)
June 19, 2019 4:10 am
#213471

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").

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Dan
June 19, 2019 7:55 am
#213472

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/

0
0
Reply
Dan (@guest_213284)
May 21, 2019 8:40 am
#213284

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?

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Dan
June 19, 2019 8:14 am
#213473

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. :(

0
0
Reply

Visit Slipstick Forums.
What's New at Slipstick.com

Latest EMO: Vol. 30 Issue 19

Subscribe to Exchange Messaging Outlook






Support Services

Do you need help setting up Outlook, moving your email to a new computer, migrating or configuring Office 365, or just need some one-on-one assistance?

Our Sponsors

CompanionLink
ReliefJet
  • Popular
  • Latest
  • Week Month All
  • Jetpack plugin with Stats module needs to be enabled.
  • Open Outlook Templates using PowerShell
  • Count and List Folders in Classic Outlook
  • Google Workspace and Outlook with POP Mail
  • Import EML Files into New Outlook
  • Opening PST files in New Outlook
  • New Outlook: Show To, CC, BCC in Replies
  • Insert Word Document into Email using VBA
  • Delete Empty Folders using PowerShell
  • Warn Before Deleting a Contact
  • Classic Outlook is NOT Going Away in 2026
Ajax spinner

Recent Bugs List

Microsoft keeps a running list of issues affecting recently released updates at Fixes or workarounds for recent issues in classic Outlook (Windows).

For new Outlook for Windows: Fixes or workarounds for recent issues in new Outlook for Windows .

Outlook for Mac Recent issues: Fixes or workarounds for recent issues in Outlook for Mac

Outlook.com Recent issues: Fixes or workarounds for recent issues on Outlook.com

Office Update History

Update history for supported Office versions is at Update history for Office

Outlook Suggestions and Feedback

Outlook Feedback covers Outlook as an email client, including Outlook Android, iOS, Mac, and Windows clients, as well as the browser extension (PWA) and Outlook on the web.

Outlook (new) Feedback. Use this for feedback and suggestions for Outlook (new).

Use Outlook.com Feedback for suggestions or feedback about Outlook.com accounts.

Other Microsoft 365 applications and services




New Outlook Articles

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Google Workspace and Outlook with POP Mail

Import EML Files into New Outlook

Opening PST files in New Outlook

New Outlook: Show To, CC, BCC in Replies

Insert Word Document into Email using VBA

Delete Empty Folders using PowerShell

Warn Before Deleting a Contact

Classic Outlook is NOT Going Away in 2026

Newest Code Samples

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Insert Word Document into Email using VBA

Warn Before Deleting a Contact

Use PowerShell to Delete Attachments

Remove RE:, FWD:, and Other Prefixes from Subject Line

Change the Mailing Address Using PowerShell

Categorize @Mentioned Messages

Send an Email When You Open Outlook

Delete Old Calendar Events using VBA

VBA Basics

How to use the VBA Editor

Work with open item or selected item

Working with All Items in a Folder or Selected Items

VBA and non-default Outlook Folders

Backup and save your Outlook VBA macros

Get text using Left, Right, Mid, Len, InStr

Using Arrays in Outlook macros

Use RegEx to extract message text

Paste clipboard contents

Windows Folder Picker

Custom Forms

Designing Microsoft Outlook Forms

Set a custom form as default

Developer Resources

Developer Resources

Developer Tools

VBOffice.net samples

SlovakTech.com

Outlook MVP David Lee

Repair PST

Convert an OST to PST

Repair damaged PST file

Repair large PST File

Remove password from PST

Merge Two Data Files

Sync & Share Outlook Data

  • Share Calendar & Contacts
  • Synchronize two computers
  • Sync Calendar and Contacts Using Outlook.com
  • Sync Outlook & Android Devices
  • Sync Google Calendar with Outlook
  • Access Folders in Other Users Mailboxes

Diane Poremsky [Outlook MVP]

Make a donation

Mail Tools

Sending and Retrieval Tools

Mass Mail Tools

Compose Tools

Duplicate Remover Tools

Mail Tools for Outlook

Online Services

Calendar Tools

Schedule Management

Calendar Printing Tools

Calendar Reminder Tools

Calendar Dates & Data

Time and Billing Tools

Meeting Productivity Tools

Duplicate Remover Tools

Productivity

Productivity Tools

Automatic Message Processing Tools

Special Function Automatic Processing Tools

Housekeeping and Message Management

Task Tools

Project and Business Management Tools

Choosing the Folder to Save a Sent Message In

Run Rules on messages after reading

Help & Suggestions

Submit Outlook Feature Requests

Slipstick Support Services

Buy Microsoft 365 Office Software and Services

Visit Slipstick Forums.

What's New at Slipstick.com

Home | Outlook User | Exchange Administrator | Office 365 | Outlook.com | Outlook Developer
Outlook for Mac | Common Problems | Utilities & Addins | Tutorials
Outlook & iCloud Issues | Outlook Apps
EMO Archives | About Slipstick | Slipstick Forums
Submit New or Updated Outlook and Exchange Server Utilities

Send comments using our Feedback page
Copyright © 2025 Slipstick Systems. All rights reserved.
Slipstick Systems is not affiliated with Microsoft Corporation.

wpDiscuz

Sign up for Exchange Messaging Outlook

Our weekly Outlook & Exchange newsletter (bi-weekly during the summer)






Please note: If you subscribed to Exchange Messaging Outlook before August 2019, please re-subscribe.

Never see this message again.

You are going to send email to

Move Comment