I met Manaswini Sawant (@manaswini) a few weeks ago on Twitter. We met when I responded to one of her tweets. The tweet in question asked Microsoft to add a new feature to Outlook. The feature Manaswini would like to see added is the ability to right-click a folder and select a menu option that would show her all the rules associated with the selected folder. It’s an interesting idea and, fortunately, one that is possible in Outlook 2007 and later with a little programming.
The code below adds an option to the menu Manaswini will see when she right-clicks a folder.
On selecting that option the code will check to see if any of her rules are related to the folder. Related in this context means that the rule uses either the “move a copy to the specified folder” or “move it to the specified folder” rule actions. Once the code has finished check the rules it will display a dialog-box that lists all the related rules or tells Manaswini that there aren’t any rules related to the folder.
Adding the Code.
Follow these instructions to add the code to Outlook.
- Open Outlook.
- Press ALT+F11 to open Outlook’s VB editor.
- If not already expanded, expand Project1.
- If not already expanded, expand Microsoft Office Outlook Objects.
- Double-click on ThisOutlookSession.
- Copy the code below and paste it into the right-hand pane of the VB editor window.
- Click the diskette icon on the toolbar to save the changes.
- Close the VB editor.
- Click Tools > Trust Center > Macro Security.
- Change the setting for Macro Security to “Warnings for all macros”.
- Click Ok.
- Close and restart Outlook.
On restarting Outlook should display a security prompt warning you that ThisOutlookSession contains macros and askign for your permission to enable them. Click the “Enable Macros” button.
Private olkCurrentFolder As Outlook.Folder Private Sub Application_FolderContextMenuDisplay(ByVal CommandBar As Office.CommandBar, ByVal Folder As Folder) Dim ofcBtn As Office.CommandBarButton Set olkCurrentFolder = Folder Set ofcBtn = CommandBar.Controls.Add(msoControlButton) With ofcBtn .Style = msoButtonIconAndCaption .Caption = "Related Rules" 'Pick a different icon from here: http://www.kebabshopblues.co.uk/2007/01/04/visual-studio-2005-tools-for-office-commandbarbutton-faceid-property/ .FaceId = 1791 .OnAction = "Project1.ThisOutlookSession.EnumerateRelatedRules" End With End Sub Sub EnumerateRelatedRules() Dim olkRul As Outlook.Rule, olkAct As Outlook.RuleAction, strRules As String For Each olkRul In Application.Session.DefaultStore.GetRules() For Each olkAct In olkRul.Actions If olkAct.ActionType = olRuleActionCopyToFolder Or olkAct.ActionType = olRuleActionMoveToFolder Then If olkAct.Enabled Then If olkAct.Folder = olkCurrentFolder Then strRules = strRules & olkRul.Name & vbCrLf End If End If End If Next Next If strRules = "" Then strRules = "None" End If MsgBox strRules, vbInformation + vbOKOnly, "Rules Related to this Folder" Set olkRul = Nothing Set olkAct = Nothing End Sub