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

VBA: Change the From Account on Replies

Slipstick Systems

› Developer › Code Samples › VBA: Change the From Account on Replies

Last reviewed on October 28, 2020     No Comments

A user wanted to always reply to messages sent to one account using another account. He also wanted to remove his address from the recipient list.

While it is much easier just to change the SMTP information on POP and IMAP accounts and use a short SendItem macro, he didn't want to change the account configuration, which he had used for years. One advantage of using this method over changing the SMTP account: he could, if desired, send the message from the old account. He also wanted the macro to work when right-clicking on a message that was not selected.

To meet his demands, I merged my 'do something when you reply macro' and a macro (by Michael Bauer) to remove the recipients.

While you can't change the sending account (using code) when you use the reading pane for replies, I was able to check for the sending account and use the reading pane for messages sent to other account. This also serves as a reminder: if the message pops out, the From address should be changed. If you are replying in the reading pane, the macro did not run.

change the account on replies

Known issues:
If you assign signatures to accounts, the signature won't be changed or added when you change the account.

If you hit reply, close the message and hit reply, reply all, or forward again, the macro won't run. It's triggered in part by a selection change, and you didn't change the selection.

By adding more If statements, you could change the account only for specific messages.

Option Explicit

Private WithEvents m_Inspectors As Outlook.Inspectors
Private WithEvents m_Inspector As Outlook.Inspector

Dim WithEvents oItem As MailItem
Dim WithEvents oExpl As Explorer
Private bDiscardEvents As Boolean
Dim oResponse As MailItem
Dim NoSendAcct As Variant
Dim SendAcct As Variant

Private Sub Application_Startup()
'MsgBox "Application_Startup running"
Set m_Inspectors = Application.Inspectors
Set oExpl = Application.ActiveExplorer
' use the name that appears in the From list
NoSendAcct = "me@domain.com"
SendAcct = "newacct@domain.com"
End Sub

Private Sub m_Inspectors_NewInspector(ByVal Inspector As Outlook.Inspector)
'MsgBox "m_Inspectors_NewInspector running"
If Inspector.CurrentItem.Class = olMailItem Then
 Set oItem = Inspector.CurrentItem
 Set m_Inspector = Inspector
End If

End Sub

Private Sub m_Inspector_Activate()
'MsgBox "m_Inspector_Activate running"

If TypeName(m_Inspector.CurrentItem) = "MailItem" And _
    m_Inspector.CurrentItem.Subject <> "" Then

Dim oResponse As MailItem
Set oResponse = m_Inspector.CurrentItem

Dim oAccount As Outlook.Account

If oResponse.SendUsingAccount = NoSendAcct Then
On Error Resume Next
For Each oAccount In Application.Session.Accounts
   If oAccount = SendAcct Then
     oResponse.SendUsingAccount = oAccount
   End If
Next
     
End If

RemoveRecipients oResponse

End If
Set m_Inspector = Nothing
End Sub

Private Sub oExpl_SelectionChange()
   On Error Resume Next
   Set oItem = oExpl.Selection.Item(1)
   
End Sub

Private Sub oItem_Reply(ByVal Response As Object, Cancel As Boolean)
If oItem.SendUsingAccount <> NoSendAcct Then Exit Sub
  
   Cancel = True
   bDiscardEvents = True

Set oResponse = oItem.Reply
 afterReply
End Sub

Private Sub oItem_Forward(ByVal Response As Object, Cancel As Boolean)
If oItem.SendUsingAccount <> NoSendAcct Then Exit Sub
   Cancel = True
   bDiscardEvents = True

Set oResponse = oItem.Forward

 afterReply
End Sub

Private Sub oItem_ReplyAll(ByVal Response As Object, Cancel As Boolean)
If oItem.SendUsingAccount <> NoSendAcct Then Exit Sub
   Cancel = True
   bDiscardEvents = True

Set oResponse = oItem.ReplyAll

 afterReply
End Sub

Private Sub afterReply()

Dim oAccount As Outlook.Account

If oResponse.SendUsingAccount = NoSendAcct Then
For Each oAccount In Application.Session.Accounts
   If oAccount = SendAcct Then
     oResponse.SendUsingAccount = oAccount
   End If
Next
     
End If

RemoveRecipients oResponse
 
oResponse.Display
 
 ' do whatever here
 
End Sub

Private Sub RemoveRecipients(Item As Outlook.MailItem)
' http://www.vboffice.net/en/developers/edit-list-of-recipients-before-sending
  Dim RemoveThis As VBA.Collection
  Dim Recipients As Outlook.Recipients
  Dim R As Outlook.Recipient
  Dim i&, y&
  Set RemoveThis = New VBA.Collection

  ' add addresses here
  RemoveThis.Add "Richard@MWWonDemand.com"
  RemoveThis.Add "richardn@ manualww.com"

  Set Recipients = Item.Recipients
  For i = Recipients.Count To 1 Step -1
    Set R = Recipients.Item(i)

    For y = 1 To RemoveThis.Count
      If LCase$(R.Address) = LCase$(RemoveThis(y)) Then
        Recipients.Remove i
        Exit For
      End If
    Next
  Next
End Sub

Private Sub m_Inspector_Close()
Set oItem = Nothing
 End Sub
Private Sub Application_Quit()
 Set m_Inspector = Nothing
 'Set objInspectors = Nothing
 Set oItem = Nothing
 End Sub

 

Reply using a shared mailbox or alias

This version of the macro replies or forwards from a another address, such as an alias or shared mailbox.

Option Explicit

Private WithEvents m_Inspectors As Outlook.Inspectors
Private WithEvents m_Inspector As Outlook.Inspector

Dim WithEvents oItem As MailItem
Dim WithEvents oExpl As Explorer
Private bDiscardEvents As Boolean
Dim oResponse As MailItem
Dim SendAcct As Variant

Private Sub Application_Startup()
'MsgBox "Application_Startup running"
Set m_Inspectors = Application.Inspectors
Set oExpl = Application.ActiveExplorer

' use the name that appears in the From list
SendAcct = "bo@domain.com"
End Sub

Private Sub m_Inspectors_NewInspector(ByVal Inspector As Outlook.Inspector)
'MsgBox "m_Inspectors_NewInspector running"
If Inspector.CurrentItem.Class = olMailItem Then
 Set oItem = Inspector.CurrentItem
 Set m_Inspector = Inspector
End If

End Sub

Private Sub m_Inspector_Activate()
'MsgBox "m_Inspector_Activate running"

If TypeName(m_Inspector.CurrentItem) = "MailItem" And _
    m_Inspector.CurrentItem.Subject <> "" Then

Dim oResponse As MailItem
Set oResponse = m_Inspector.CurrentItem

Dim oAccount As Outlook.Account

If oResponse.SendUsingAccount = SendAcct Then
On Error Resume Next
oResponse.SentOnBehalfOfName = "olsales@domain.com"
End If

End If
Set m_Inspector = Nothing
End Sub

Private Sub oExpl_SelectionChange()
   On Error Resume Next
   Set oItem = oExpl.Selection.Item(1)
   
End Sub

Private Sub oItem_Reply(ByVal Response As Object, Cancel As Boolean)
If oItem.SendUsingAccount <> SendAcct Then Exit Sub
  
   Cancel = True
   bDiscardEvents = True

Set oResponse = oItem.Reply
afterReply

End Sub

Private Sub oItem_Forward(ByVal Response As Object, Cancel As Boolean)
If oItem.SendUsingAccount <> SendAcct Then Exit Sub
   Cancel = True
   bDiscardEvents = True

Set oResponse = oItem.Forward
afterReply

End Sub

Private Sub oItem_ReplyAll(ByVal Response As Object, Cancel As Boolean)
If oItem.SendUsingAccount <> SendAcct Then Exit Sub
   Cancel = True
   bDiscardEvents = True

Set oResponse = oItem.ReplyAll
afterReply
End Sub

Private Sub afterReply()

Dim oAccount As Outlook.Account

If oResponse.SendUsingAccount = SendAcct Then
    oResponse.SentOnBehalfOfName = "sales@domain.com"
End If

oResponse.Display

End Sub

Private Sub m_Inspector_Close()
Set oItem = Nothing
 End Sub
Private Sub Application_Quit()
 Set m_Inspector = Nothing
 'Set objInspectors = Nothing
 Set oItem = Nothing
 End Sub

How to use the macros on this page

First: You need to have macro security set to the lowest setting, Enable all macros during testing. The macros will not work with the top two options that disable all macros or unsigned macros. You could choose the option Notification for all macros, then accept it each time you restart Outlook, however, because it's somewhat hard to sneak macros into Outlook (unlike in Word and Excel), allowing all macros is safe, especially during the testing phase. You can sign the macro when it is finished and change the macro security to notify.

To check your macro security in Outlook 2010 and newer, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, look 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.

Macros that run when Outlook starts or automatically need to be in ThisOutlookSession, all other macros should be put in a module, but most will also work if placed in ThisOutlookSession. (It's generally recommended to keep only the automatic macros in ThisOutlookSession and use modules for all other macros.) The instructions are below.

The macros on this page need to go into ThisOutlookSession.

Open the VBA Editor by pressing Alt+F11 on your keyboard.

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

More information as well as screenshots are at How to use the VBA Editor

VBA: Change the From Account on Replies was last modified: October 28th, 2020 by Diane Poremsky

Related Posts:

  • Macro to Reply, ReplyAll, or Forward and File
  • VBA Sample: Do Something When Reply is Clicked
  • Use a Default Subject for New Messages
  • Copy attachment names when replying

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
0 Comments
newest
oldest most voted
Inline Feedbacks
View all comments

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

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