A user wanted "a rule that will perform an action if I am the only one in the To box, even when there are others in the CC box". He also asked for a rule "that will perform an action if I am included in the To box but am not the only one in the To box".
While Outlook's Rules can't do this on their own, you can do this using a run a script rule. The script counts the number of recipients, checks to see how many are in the To field and gets the addresses before checking to see if your address is in the To field.
Note: the address on an Exchange server mailbox is not your SMTP address. The macro will print the Exchange address to the Immediate window (Ctrl+G to show it). Copy a portion of your alias from the right end of the address.
Are you the only address in the To field?
This script checks to see if there is more than one recipient on a message and if so, is your address the only address in the To field. If it's the only address in the To field, it "does something", in this case, moves to the message to a folder. If there is more than one address in the To field, it does something else with the message (moves to a different folder in this example.)
To use, add the script to the Visual Basic Editor then create a rule. Note that only the run a script action should be in the rule, the actions you want to apply to the message need to be in the script.
Sub MeTo(Item As Outlook.MailItem) Dim toCount As Long Dim moveFolder As Folder ' see if there is more than 1 recipient If Item.Recipients.Count = 1 Then Exit Sub For Each Recipient In Item.Recipients Debug.Print Recipient.Address, Recipient.Type If (Recipient.Type = olTo) Then toCount = toCount + 1 strRecip = Recipient.Address & ";" & strRecip End If Next Recipient Debug.Print strRecip ' only need enough of your address to be unique ' if using Exchange, you need to check for the internal address. 'If InStr(1, strRecip, "efudd@slipstick") Then If InStr(1, strRecip, "670-efudd") Then If toCount > 1 Then ' do something when others are in To 'MsgBox "I'm not alone in the To field" 'Debug.Print "I'm not alone in the To field" Set moveFolder = Session.GetDefaultFolder(olFolderInbox).Folders("Others") Item.Move moveFolder ElseIf toCount = 1 Then 'MsgBox "I'm alone in the To field" 'Debug.Print "I'm alone in the To field" Set moveFolder = Session.GetDefaultFolder(olFolderInbox).Folders("Only Me") Item.Move moveFolder End If End If Set moveFolder = Nothing End Sub
Your address is in the To field
This script doesn't really do more than what the rules can do already unless you need to do something not available in Rules Actions.
Sub MeTo(Item As Outlook.MailItem) For Each Recipient In Item.Recipients Debug.Print Recipient.Address, Recipient.Type If (Recipient.Type = olTo) Then strRecip = Recipient.Address & ";" & strRecip End If Next Recipient Debug.Print strRecip ' only need enough of your address to be unique ' if using Exchange, you need to check for the internal address. ' If InStr(1, strRecip, "efudd@slipstick") Then If InStr(1, strRecip, "670-efudd") Then ' do something MsgBox "I'm in the To field" Debug.Print "I'm in the To field" End If End Sub
Test the Rule without sending messages
To test the rule without sending yourself mail, you can select a message and use this macro to run the script on the selected message.
Sub TestRule() 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 MeTo 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