Finding Rules Related to a Folder in Outlook


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.

Screenshot of the modified right-click menu

Modified right-click menu

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.

Screenshot of the dialog-box showing the rules related to the selected folder

Related Rules Dialog-box

Adding the Code.

Follow these instructions to add the code to Outlook.

  1. Open Outlook.
  2. Press ALT+F11 to open Outlook’s VB editor.
  3. If not already expanded, expand Project1.
  4. If not already expanded, expand Microsoft Office Outlook Objects.
  5. Double-click on ThisOutlookSession.
  6. Copy the code below and paste it into the right-hand pane of the VB editor window.
  7. Click the diskette icon on the toolbar to save the changes.
  8. Close the VB editor.
  9. Click Tools > Trust Center > Macro Security.
  10. Change the setting for Macro Security to “Warnings for all macros”.
  11. Click Ok.
  12. 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

Leave a comment