A user wanted to know how to create an AND rule:
If the subject contains outlook AND word, do something
For example: "outlook AND word" to match on a Subject line of "Mail merge Outlook and Word", but not "Outlook Teams Meeting"
While Outlook Rules don't support AND within a rule, you can make it work using two rules. The first rule looks for one word, sets a category, and does not use stop processing more rules. The second rule looks for the second word (and can clear the category field).
Step 1: Create a rule that looks for one of the words and add a category if the word is found. Do not use Stop Processing.
Step 2: Create a second rule to look for the other word. You can use stop processing in this rule.
The result: all messages with those two words in the subject are moved.
Use a Run a Script Rule
Instead of using two rules, you can a run a script rule to check for the presence of both words.
You will need to set a registry key to enable the Run a script option in Rules & Alerts in Outlook 2013 and newer. See Run-a-Script Rules Missing in Outlook for more information and the registry key to fix restore it.
Sub ANDRules(Item As Outlook.MailItem) If InStr(1, LCase(Item.Subject), "outlook") > 0 And InStr(LCase(Item.Subject), "word") > 0 Then ' do something ' use these actions for testing purposes MsgBox "We have a match!!! " & Item.Subject Else MsgBox "No Match :( " & Item.Subject End If End Sub
To test the rule without needing to send messages to yourself, select a message and run this script.
' use this to test the script - select a message and run this macro Sub RunAndRules() 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 ANDRules 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.
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.
More information as well as screenshots are at How to use the VBA Editor