Use this code to send a new message using the address the selected message was sent To as the send using From account.
See Using a Gmail Master Account: Reply using the Correct Account for instructions and an automated set of macros. It will work with any service that collects mail sent to other accounts.
This code is for people who use an account such as Gmail or Outlook.com to collect mail from other accounts and download only that account. Outlook will always reply using the account that downloaded the message, which is the Outlook.com or Gmail account. You'll need to create an account in Outlook for the other addresses to use this code, but can set the accounts to never download email.
This code gets the address the message was sent to and looks for an existing account with the same display name and uses that account. If it can't find a matching account it uses the account that downloaded the message. This could be changed to use the default account in your profile.
Update: I fixed the macro, it now checks for a specific string (an email address) in the To field. If it finds the address, the reply is from that address, otherwise the reply is from the default address. If you need to check for one of several accounts, you'll need to use an array.
To force the code to always use a specific non-default account, change this line:
objMsg.SendUsingAccount = olNS.Accounts.Item(1) to
objMsg.SendUsingAccount = olNS.Accounts.Item(2) where 2 is the placement of the account in the Account Settings list
or objMsg.SendUsingAccount = "Display name of desired account"
If you are using Exchange server and have Send as permission to the address a message was sent to (such as a distribution list), replace the For Each... Next block with:
objMsg.MailItem.Sender = oMail.To
or
objMsg.SentOnBehalfOfName = oMail.To
This may not work with Outlook 2013's "inline replies". Messages will be opened in a new Window.
Reply from the address a message was sent to
Public Sub AccountSelection()
Dim oAccount As Outlook.Account
Dim strAccount As String
Dim olNS As Outlook.NameSpace
Dim objMsg, oMail As MailItem
Set olNS = Application.GetNamespace("MAPI")
' For a reply all version, replace Reply with ReplyAll
Set objMsg = ActiveExplorer.Selection.Item(1).Reply
If TypeName(ActiveExplorer.Selection.Item(1)) = "MailItem" Then
Set oMail = ActiveExplorer.Selection.Item(1)
On Error Resume Next
For Each Recipient In oMail.Recipients
strRecip = Recipient.Address & ";" & strRecip
Next Recipient
If InStr(strRecip, "alias@domain1.com") = 1 Then
' StrAccount is the account name as shown in the Account Settings list
strAccount = "alias@domain1.com"
Else
End If
For Each oAccount In Application.Session.Accounts
If oAccount.DisplayName = strAccount Then
objMsg.SendUsingAccount = oAccount
Else
' to reply using the account that downloaded the message
' leave the objMsg line commented
' remove comment to reply using default account
' objMsg.SendUsingAccount = olNS.Accounts.Item(1)
End If
Next
objMsg.Display
Else
End If
Set objMsg = Nothing
Set olNS = Nothing
End Sub
Check for multiple aliases
This version of the macro, updated by Victor Beekman, checks for multiple aliases.
Public Sub AccountSelection()
Dim oAccount As Outlook.Account
Dim strAccount As String
Const strAlias1 = "alias1@somedomain.xxx"
Const strAlias2 = "alias2@somedomain.xxx"
Dim strRecip As String
Dim olNS As Outlook.NameSpace
Dim objMsg, oMail As MailItem
Dim Recips As Object
Dim i As Long
Set olNS = Application.GetNamespace("MAPI")
' to integrate with replyall button see https://www.slipstick.com/developer/remove-address-reply/
' For a reply all version, replace Reply with ReplyAll
Set objMsg = ActiveExplorer.Selection.Item(1).ReplyAll
On Error Resume Next
Set Recips = objMsg.Recipients
For i = Recips.Count To 1 Step -1
If LCase(Recips.Item(i).Address) = strAlias1 Then
strAccount = strAlias1
Recips.Remove i
ElseIf LCase(Recips.Item(i).Address) = strAlias2 Then
strAccount = strAlias2
Recips.Remove i
End If
Next i
If InStr(strRecip, strAlias1) = 1 Then
' StrAccount is the account name as shown in the Account Settings list
strAccount = strAlias1
ElseIf InStr(strRecip, strAlias2) = 1 Then
strAccount = strAlias2
End If
For Each oAccount In Application.Session.Accounts
If oAccount.DisplayName = strAccount Then
objMsg.SendUsingAccount = oAccount
Else
' to reply using the account that downloaded the message
' leave the objMsg line commented
' remove comment to reply using default account
' objMsg.SendUsingAccount = olNS.Accounts.Item(1)
End If
Next
objMsg.Display
Set objMsg = Nothing
Set olNS = Nothing
End Sub
How to use macros
First: You will need macro security set to low during testing.
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.
Open the VBA Editor by pressing Alt+F11 on your keyboard.
To put the code in a module:
- Right click on Project1 and choose Insert > Module
- Copy and paste the macro into the new module.
- Click Run to test the macro
When you're happy with the macro, create a ribbon or QAT button for the macro.
More information as well as screenshots are at How to use the VBA Editor
Estelle says
Hi Diane, I have 2 outlook accounts including 1 imap account that is automatically transferred to my outlook.com account.
I would like that when I click on reply to an email sent to my imap account in my outlookaccount folder that the from field is "imap account" instead of exchange account and that I have a BCC field at a third address. I use in thisoutlook session a code that works for the BCC without worry.
What can I do?
I also used a module but it requires me to click on the module instead of "reply to" which is not very convenient. Thanks in advance.
Translated with http://www.DeepL.com/Translator (free version)
Paolo says
Hi Diane, I try to use this macro to set the from field with the distribution group address as you suggest using Outlook 2013 on Windows 7 pro 32 bit, (the formsadmin doesn't work well with it), but the field remains the same.
Thanks for your time.
Diane Poremsky says
in exchange, you need to use the exchange alias or x500 address here:
If InStr(strRecip, "alias@domain1.com")
This will get the sender's address from a selected item. I prefer using /cn=alias part instead of just alias, but it can include a long string of number, at least with office 365 mailboxes.
Public Sub Getx500Address()
Dim objMail As Object
Set objMail = Application.ActiveExplorer.Selection.Item(1)
MsgBox objMail.SenderEmailAddress
Set objMail = Nothing
End Sub
Tim says
Hi
This Macro works well in specifying the correct account - thank you, however it does not pick up the Signature for that account in the reply text.
It still uses the default Signature.
Any thoughts on how to change the macro so it selects the correct Signature ?
Thanks
Diane Poremsky says
You'll need to insert it using the macro - I don't have any code handy that does that though.
Fabio says
Ok... Now you got me, what's an inspector?
Diane Poremsky says
it's how vba refers to an open item. A code example is here - https://www.slipstick.com/developer/vba-disable-outlook-2010-no-subject-warning/
this sample works with open or close: https://www.vboffice.net/en/developers/inspector-wrapper-receive-events-of-multiple-emails
Fabio says
Duh!...thanks Diane...I'd probably still be typing ".Category"...for those of you who want to know. I added the following to this macro.
' Set Category
objMsg.Categories = "Sent By Fabio"
objMsg.Save
The reason I did it was due to the fact that my company is using Google Apps and because I'm using outlook in my office PC and my laptop, I have to use the "recent:" setting (recent:fabio@mydomain.com) on both machines, so I can make sure I get emails in both machines. Google mail seems to ignore the POP setting to leave messages on the server so it can be retrieved by another machine and IMAP doesn't work too well. The problem is that you get a duplicate of every email you sent into your inbox and when you're responding to lots of emails from clients (along with other co-workers using the same email aliases --- we really need a ticketing system but the boss thinks it's unnecessary). Anyway, I needed to identify the messages I sent versus the messages my co-workers sent, so I couldn't filter via the incoming email address otherwise I might miss one of their responses. So assigning a category only to the messages I sent allows one of my rules to delete incoming messages with this category, after all I already have a copy of the message I sent in my sent items folder -- I don't need another copy!
Now if, I could auto assign a category every time I click a NEW email so my rule would work for my own messages (not using one of the aliases) everything would be perfect. Still, I can at least filter messages coming from my own email in a separate rule, so it's not so bad.
Diane Poremsky says
you can but need to use an inspector. i'll see if i can find a code sample, im sure i have something here. Or you could use a macro instead of the New button...
Fabio says
To expand on this Macro, I wanted to include a specific Category every time I send from this Macro but I can't seem to do it. I thought it was simple as:
.Category = "Sent from Reply Macro"
Obviously the Category has already been created. I saw a few examples where a macro can display a list of categories to choose from but I can't seem to just set one without having to choose.
I'm not an expert on vb, so that might be the problem ;-)
-Fabio
Diane Poremsky says
it's .categories, not .category.
Michael says
Diane, Found one more weird thing. If the email is sent to me ONLY (example To: a@b.com) the macro works.
However, if it is To: a@b.com; differentemail@c.com then it does not work. As in, if my email is only one of multiple addresses it does not work.
What would I need to modify to say if my email address is anywhere in the To: field then apply this rule?
Diane Poremsky says
This: If InStr(strRecip, "alias@domain1.com") = 1 Then detects your address within the recipient string. Try > 0 instead of = 1
Is the case the same each time? If not, use the lcase function to force it to lower case - InStr(lcase(strRecip), "alias@domain1.com")
Michael says
That fixed it!!! Thanks so much
Michael says
Did you retest? I have now tried on three different computers with the most recent Outlook 2013 with all updates on Windows 8.1. None of them are working.
Diane Poremsky says
Change the last Dim line to this: Dim objMsg As MailItem, oMail As MailItem - the missing as Mailitem is the problem.
Michael says
Thanks so much!!! If there is anything I can do to provide data I will -- just shoot me an email and I can send screenshots or anything.
Michael says
Are you using Office 2013? I just can't seem to make it work no matter what I do!
Diane Poremsky says
It was tested in both 2010 and 2013. I'll retest it, it's possible an update changed something - another person is having problems using accounts in a macro too.
Fabio Quintal says
I know this is an old post but I wanted to thank Diane for posting this macro.
Diane:
This worked perfectly for me, I tend to read most of my mail on the default reading pane (as opposed to opening the email completely -which doesn't work). I actually created three different Macros for REPLY, REPLY ALL, and FORWARD and replaced the buttons on the ribbon so I wouldn't click them by mistake. The only change (besides the obvious changes) I added a line "strRecip = LCase(strRecip)" because I noticed the Macro would not work correctly if the person used uppercase when typing the email address...for example if they typed it as info@mydomain.com it would work fine but if they typed it as Info@mydomain.com or INFO@mydomain.com or even the domain itself it would NOT work. With this addition everything works!
Ascar Omarov:
In case you're still looking for the Forward...just replace the line:
Set objMsg = ActiveExplorer.Selection.Item(1).Reply
with
Set objMsg = ActiveExplorer.Selection.Item(1).Forward
And everything should work for forwarding.
Ascar Omarov says
Derik, may I ask you to share the modified code which includes the Forward function? Did you try to make the Reply All and Forward as Attachment functions to work as well?
Derik du Plessis says
Diane, i have just experiment more, the above indicated actually has nothing to do with the macros, instead by changing the option indicated this affected an add in that i have called SAM, and that activates the correct sending account. Sorry about the confusion.
Regards
Diane Poremsky says
Yeah, I wasn't thinking either. I was trying to automate it last night so it would capture the Reply button and that will not work with inline replies (that is a limitation of the new feature) - but that macro would be totally different than this one (and it was not working as expected either).
Derik du Plessis says
As i do not like answering email or creating new ones in the actual reading pane, i changed the setting in options/mail/replies and forwards to always open in a new window (first check box) and now the macros run automatically and i am always presented with the correct mail account to use when answering emails. This works for both the ribbon as well as inline "Reply", "Reply All" and "Forward" buttons. Please do not ask me why, as MS is sometimes a black art.
Any idea how i can get the same funcionality when creating a new meeting invitation?
Diane Poremsky says
A lot of macros won't work on inline replies - it's how the inline replies are handled in the object model.
I'm not sure I understand the meeting question.
Derik du Plessis says
This is great as it is exactly what i needed. I also modified the macro to include a forward version. However the macros does not run when i press the Reply, Reply All or Forward buttons either on the ribbon or the "inline" buttons they do run successfully when i execute them from the developer pane or the macro pane. How can i get them to run automatically?
Once again, thanks for this great macro!!
Diane Poremsky says
You need to create a ribbon button for the macro - it doesn't watch for the buttons to be clicked.
Peauxboy says
This is EXACTLY what I am looking for!!! I think. I'd like to use this so that customer service representatives always respond with my companies support email when responding to messages in a specific public folder instead of their own without requiring them to remember to change the From field. My only problem is how do I implement this? In other words, what do I need to do so that this is used?
Thanks so much!
Diane Poremsky says
Because its a public folder, i don't think that one will work. It looks at accounts in the Accounts list. You either need to use a macro with the MailItem.Sender Property or the choose from account macro.