I receive messages from a group address. The name in the From field is the team name and address. In the message header there are some specific words I can search for that identify the actual sender, for example
From: Mike <email@example.com>. Is it possible to set up a rule to see "Mike on behalf of Team" in the From field of these messages?
You can use a 'words in the header' rule to find a phrase as it appears in the header but you can't change what you see in the From field using straight VBA. It's not impossible to change, but you'll need to use Redemption (What is Redemption) to make changes to the display name (and any other field not writable by VBA.)
Sample code using redemption is below. You can look at the values using MFCMAPI to see what Outlook has for the sender name and representing name and see if both need changed or if you can use one value in the other field.
After installing Redemption and selecting it in Tools, References, run this code to change the Sender's name to whatever you want. You'll need additional code to grab the name from the header.
The code sample at Get Outlook's Internet Headers using VBA can get the value from the header and the sample at Read MAPI properties not exposed in Outlook's Object Model shows how to get Outlook property values. To change the display name to match the name on the sender's contact, the code at Assign Categories to Messages using Contact Category shows how to do a Contact lookup.
Sub ChangeName() Dim rSession As Redemption.RDOSession Dim rMail As Redemption.RDOMail Dim ID As String Const PR_SENDER_NAME = &HC1A001E Const PR_SENT_REPRESENTING_NAME = &H42001E Set rSession = CreateObject("Redemption.RDOSession") rSession.Logon ID = Application.ActiveExplorer.Selection.Item(1).EntryID Set rMail = rSession.GetMessageFromID(ID, Application.Session.DefaultStore.StoreID) rMail.Fields(PR_SENDER_NAME) = "New Name" rMail.Fields(PR_SENT_REPRESENTING_NAME) = "New Name" rMail.Save rSession.Logoff End Sub
Change the display name to match the Contact's Full Name
This example shows how to use the code to lookup a contact and get the full name to use as the sender name.
Public Sub ChangeSenderName() Dim objMail As Object Dim oContact As Outlook.ContactItem Dim oSender Dim strName As String Set objMail = Application.ActiveExplorer.Selection.Item(1) Set oSender = objMail.Sender If Not oSender Is Nothing Then Set oContact = oSender.GetContact Set oSender = Nothing If Not oContact Is Nothing Then strName = oContact.FullName Set oContact = Nothing End If End If Dim rSession As Redemption.RDOSession Dim rMail As Redemption.RDOMail Dim ID As String Const PR_SENDER_NAME = &HC1A001E Const PR_SENT_REPRESENTING_NAME = &H42001E Set rSession = CreateObject("Redemption.RDOSession") rSession.Logon ID = Application.ActiveExplorer.Selection.Item(1).EntryID Set rMail = rSession.GetMessageFromID(ID, Application.Session.DefaultStore.StoreID) rMail.Fields(PR_SENDER_NAME) = strName rMail.Fields(PR_SENT_REPRESENTING_NAME) = strName rMail.Save rSession.Logoff 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 should be placed in a module.
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.
Set a reference to other Object Libraries
To use this macro, you need to set a reference to the Redemption object. If you receive a "User-defined type not defined" error, you did not the set reference.
- Go to Tools, References menu.
- Locate the Redemption object library in the list and add a check mark to it.
More information as well as screenshots are at How to use the VBA Editor
Code samples to get the value from the header or other fields: