A user wanted to know how to create a rule to add a category to messages he was @mentioned on. Although I’m not sure this is all that useful as Outlook has a filter for showing only messages where you were @mentioned, it is possible to use a run a script rule or an ItemAdd macro to set a category on a message you were @mentioned on if you are using classic Outlook.
While my sample shows how to add a category, it could move the message or flag it and set a reminder.
Run a Script Rule
This macro can be placed in a module. You need to edit the registry to enable scripts in rules.
For more information on Run a Script rules, see these articles:
Outlook's Rules and Alerts: Run a Script
Run-a-Script Rules Missing in Outlook
Private Sub (ByVal Item As Object) Dim propertyAccessor As Outlook.propertyAccessor Set propertyAccessor = Item.propertyAccessor 'Mentioned 'http://schemas.microsoft.com/mapi/string/{41F28F13-83F4-4114-A584-EEDB5A6B0BFF}/IsMentioned/0x0000000B Dim prop On Error GoTo ErrorHandler prop = propertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/string/{41F28F13-83F4-4114-A584-EEDB5A6B0BFF}/IsMentioned/0x0000000B") If IsError(prop) Then Exit Sub 'if you want to flag any message containing an @mention, change to False. If prop = True Then Item.Categories = "@Mentioned" Item.Save End If ErrorHandler: Exit Sub End Sub
Watch a folder using ItemAdd
This macro runs on new messages added to the folder. It needs to be in ThisOutlookSession.
For more information on ItemAdd macros, see How to use an ItemAdd Macro
Option Explicit Private objNS As Outlook.NameSpace Private WithEvents objItems As Outlook.Items Private Sub Application_Startup() Dim objWatchFolder As Outlook.Folder Set objNS = Application.GetNamespace("MAPI") 'Set the folder and items to watch: Set objWatchFolder = objNS.GetDefaultFolder(olFolderInbox) Set atMention = objWatchFolder.Items Set objWatchFolder = Nothing End Sub Private Sub atMention_ItemAdd(ByVal Item As Object) Dim propertyAccessor As Outlook.propertyAccessor Set propertyAccessor = Item.propertyAccessor 'Mentioned 'http://schemas.microsoft.com/mapi/string/{41F28F13-83F4-4114-A584-EEDB5A6B0BFF}/IsMentioned/0x0000000B Dim prop On Error GoTo ErrorHandler prop = propertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/string/{41F28F13-83F4-4114-A584-EEDB5A6B0BFF}/IsMentioned/0x0000000B") If IsError(prop) Then Exit Sub 'if you want to flag any message containing an @mention, change to False. If prop = True Then Item.Categories = "@Mentioned" Item.Save End If ErrorHandler: Exit Sub End Sub
Testing the macros
Use this macro to test the run a script or ItemAdd macro on an existing item in your Inbox. Select the message then run the macro.
Sub RunAutoReplywithTime() Dim objApp As Outlook.Application Dim objItem As Object ' MailItem Set objApp = Application Set objItem = objApp.ActiveExplorer.Selection.Item(1) 'macro name you want to run goes here AtMentioned objItem 'atMention_ItemAdd objItem 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.
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.
To put the macro code in ThisOutlookSession:
- Expand Project1 and double click on ThisOutlookSession.
- 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