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
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)
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.
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
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
You'll need to insert it using the macro - I don't have any code handy that does that though.
Ok... Now you got me, what's an inspector?
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
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… Read more »
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...
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
it's .categories, not .category.
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?
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")
That fixed it!!! Thanks so much