Create a List of Rules

Chia sẻ bởi:hands
★★★★★
Quảng cáo

Applies to: Outlook 2016 (Win), Outlook 2013, Outlook 2010, Outlook 2007
Valk Beekman wanted to share this VBA code sample that creates a text file containing a list of the rules used by the default email account. When the rule contains an email address, the address (or display name) is added to the output. It needs a little tweaking to list all the actions and conditions in a rule, but for those who just want a list of rules, in the order they are listed in Rules and Alerts, it works very well as is.
Note: when I tested this it would not work in my profile containing several Exchange accounts. It will work with POP3, and IMAP (at least in Outlook 2007/2010)

Note that this works with Outlook 2007 and up. It will not work with older versions of Outlook.

Sample Results

The VBA below creates the list of rules assigned to the default email account. If you are editing rules to test the macro, make sure you are working with the correct set of rules in Rules & Alerts.
[URL='cdn.slipstick.com/images/2012/01/rules-alerts-dialog.png']cdn.slipstick.com/images/2012/01/rules-alerts-dialog-575×246.png

The resulting list is in this format:

1  Rule Name:Clear categories on mail (recommended)
2  Rule Name:Tips  Folder path:\Personal FoldersInboxtest  From:tips@outlook-tips.net
3  Rule Name:Clear categories on mail (recommended)
4  Rule Name:Autoaccept rule  Subject:'Autoaccept rule'
5  Rule Name:Test  Subject:'Test'
6  Rule Name:'?' or '?' or '?' or '?'  Body Or Subject:'?' '?' '?' '?'
7  Rule Name:Test 3  From:alias@gmail.com
8  Rule Name:DServices  Folder path:\Personal Folderstestt5  From:domain@mail.com
9  Rule Name:Test1  From:sally@smith.com
10  Rule Name:Test 2  From:steve@handyman.com
11  Rule Name:Follow up  From:microsoft.com

Notes: The output does not support Unicode text as rule #5 looks for four Chinese characters (in the subject or body and deletes the message as spam).

VBA Macro to List Rules

This is not the most efficient code, but it works. I tested this VBA in Outlook 2007 with only an IMAP email account in the profile. It would not work in Outlook 2010 with an Exchange server account set as default (and several accounts in the profile). I'm not sure why yet but it works in Outlook 2010 with other profiles.

Sub ListRules()
 Dim colStores As Outlook.Stores
    Dim oFileSys As Object
    Dim oStore As Outlook.Store
    Dim oRoot As Outlook.Folder
    Dim oCondition As Outlook.RuleCondition
    Dim oCondfrom As Outlook.RuleCondition
    Dim oAction As Outlook.RuleAction
    Dim colRules As Object
    Dim oRule As Outlook.Rule
    Dim oInbox As Outlook.Folder
    Dim oMoveTarget As Outlook.Folder
    Dim sOutput As String
    Dim myVar As Variant

'On Error Resume Next
    Set oFileSys = CreateObject("Scripting.FileSystemObject")

'Create a folder named Rules on your C drive or change the path to use an existing folder
    If oFileSys.FileExists("C:RulesOLfilterlist.txt") Then
        oFileSys.DeleteFile ("C:RulesOLfilterlist.txt")
    End If
    Open "C:RulesOLfilterlist.txt" For Output As #1

Set colStores = Application.Session.Stores
    Set oStore = colStores(1)
        Set oRoot = oStore.GetRootFolder
        Set colRules = oStore.GetRules

For Each oRule In colRules
sOutput = (oRule.ExecutionOrder) & Chr(9) & "Rule Name:" & (oRule.Name)
 For Each oAction In oRule.Actions
  If oAction.Enabled = True Then
    If oAction.ActionType = olRuleActionMoveToFolder Then
     sOutput = sOutput & Chr(9) & "Folder path:" & (oAction.Folder.FolderPath)
    End If

'add more actions here

End If
 Next

For Each oCondition In oRule.Conditions
    If oCondition.Enabled = True Then
      If oCondition.ConditionType = olConditionFrom Then
        sOutput = sOutput & Chr(9) & "From:" & (oRule.Conditions.From.Recipients(1))
      End If

'add more conditions here

End If
 Next
            Print #1, sOutput
         Next
    Close #1
End Sub

Adding More Conditions and Actions

The basic macro was written for a specific purpose – to get the From conditions and the folder used in the Move to folder action. Adding more conditions and actions is very easy, although it does require a little knowledge of how to use the VBA Editor.

Below is a sample of the VBA needed to get the Subject, Body, and message header rules. The remaining conditions and actions will use similar code.

To use these with the code above, paste them where the 'add more conditions here' line is.

'Words in the Subject       
If oCondition.ConditionType = olConditionSubject Then

'use this format when the condition may contain multiple values
   sOutput = sOutput & Chr(9) & "Subject:"
      For Each myVar In oRule.Conditions.Subject.Text
        sOutput = sOutput & "'" & myVar & "' "
     Next
End If

'Words in the body
If oCondition.ConditionType = olConditionBody Then
   sOutput = sOutput & Chr(9) & "Body:" & (oRule.Conditions.Body.Text)
End If

'Message header contains
If oCondition.ConditionType = olConditionMessageHeader Then
   sOutput = sOutput & Chr(9) & "Header:" & (oRule.Conditions.MessageHeader.Text)
End If

'body or subject
If oCondition.ConditionType = olConditionBodyOrSubject Then
  sOutput = sOutput & Chr(9) & "Body Or Subject:"
     For Each myVar In oRule.Conditions.BodyOrSubject.Text
        sOutput = sOutput & "'" & myVar & "' "
     Next
End If

An alternative format for the text file is available in the macro here: [URL='www.slipstick.com/code-samples/listrules-block.txt']ListRules – Block format. This code sample contains additional conditions and actions.
The results will be in this format:

1  Rule Name:This is a test
  Flagged:True
  Copy to:\dianep@domain.comJunk E-mail

2  Rule Name:Any
  Flagged:True
  Forward:True

Conditions
Actions

olConditionAccount
olConditionAnyCategory
olConditionBody
olConditionBodyOrSubject
olConditionCategory
olConditionCc
olConditionDateRange
olConditionFlaggedForAction
olConditionFormName
olConditionFrom
olConditionFromAnyRssFeed
olConditionFromRssFeed
olConditionHasAttachment
olConditionImportance
olConditionLocalMachineOnly
olConditionMeetingInviteOrUpdate
olConditionMessageHeader
olConditionNotTo
olConditionOnlyToMe
olConditionOOF
olConditionOtherMachine
olConditionProperty
olConditionRecipientAddress
olConditionSenderAddress
olConditionSenderInAddressBook
olConditionSensitivity
olConditionSentTo
olConditionSizeRange
olConditionSubject
olConditionTo
olConditionToOrCc
olConditionUnknown
olRuleActionAssignToCategory
olRuleActionCcMessage
olRuleActionClearCategories
olRuleActionCopyToFolder
olRuleActionCustomAction
olRuleActionDefer
olRuleActionDelete
olRuleActionDeletePermanently
olRuleActionDesktopAlert
olRuleActionFlagClear
olRuleActionFlagColor
olRuleActionFlagForActionInDays
olRuleActionForward
olRuleActionForwardAsAttachment
olRuleActionImportance
olRuleActionMarkAsTask
olRuleActionMarkRead
olRuleActionMoveToFolder
olRuleActionNewItemAlert
olRuleActionNotifyDelivery
olRuleActionNotifyRead
olRuleActionPlaySound
olRuleActionPrint
olRuleActionRedirect
olRuleActionRunScript
olRuleActionSensitivity
olRuleActionServerReply
olRuleActionStartApplication
olRuleActionStop
olRuleActionTemplate
olRuleActionUnknown

Using the VBA Editor

Copy and paste the VB code into Outlook's VB Editor. You'll also need to allow macros to run. If you aren't asked if you want to allow macros when you open the VB editor, you'll need to change the macro security in Options, Trust Center to 'Always ask' about macros. We do not recommend the lowest security setting to never ask about macros.

When you paste the code, the text colors should be the same as seen in the code above. Red lines mean there is an error in the code.

  • Press ALT+F11 to open Outlook's VB editor
  • If asked, you need to enable Macros
  • Expand Project1 and Microsoft Office Outlook Objects
  • Double-click on ThisOutlookSession
  • Copy the code and paste it into the VB editor window (right pane)
  • Save the changes (Ctrl+S or click the Save icon)
  • Press F8 or the Run button to run the macro

For more information, see [URL='www.slipstick.com/developer/how-to-use-outlooks-vba-editor/']How to use Outlook's VBA Editor

[URL='www.slipstick.com/outlook/rules/create-list-rules/#tools']
Tools

[URL='https://www.sperrysoftware.com/Email-Tools/product/power-rules-manager/?wpam_id=6']Power Rules Manager

Sperry Software's Power Rules Manager, an add-in that allows you to view and edit your rules from a grid window, received a small but significant upgrade: You can now drag and drop Outlook rules right in the grid window for simple control over Outlook rule execution order. And, if you hold down the Shift key while the grid is scrolling as you drag your rules, it goes into "turbo mode" in case you have hundreds (or even thousands) of rules. It also now has several other nice enhancements including support for non-default sets of rules.

[URL='https://www.sperrysoftware.com/Email-Tools/product/Timed-Email-Organizer/?wpam_id=6']Timed Email Organizer

Timed Email Organizer is a brand new add-in which can replace or augment your Outlook rules. Unlike Outlook, this add-in will act on emails based on how old they are, supports ANDs, ORs, NOTs and wildcards in the conditions, and has a test mode so that you can see what the effect of a given rule would be if it were run. It will even import your current Outlook rules and automatically disable them for you.

More Information
Another macro that can be useful when troubleshooting rules is [URL='techniclee.wordpress.com/2011/06/18/finding-rules-related-to-a-folder-in-outlook/']Finding Rules Related to a Folder in Outlook. This macro, written by Outlook Developer MVP David Lee, adds the ability to right-click on a folder and see all of the rules associated with the selected folder. (Outlook 2007 and later.)
[URL='docs.microsoft.com/en-us/office/vba/api/Outlook.OlRuleActionType']OlRuleActionType Enumeration (Outlook)

www.slipstick.com/outlook/rules/create-list-rules/

Thiết kế Tổng đãi ngộ (Total Rewards) theo khung SHRM
Khóa học SprinGO phù hợp

Thiết kế Tổng đãi ngộ (Total Rewards) theo khung SHRM

Khóa học “Thiết kế Tổng phần thưởng (Total Reward) chuẩn khung SHRM” giúp bạn nắm vững toàn bộ hệ thống đãi ngộ theo chuẩn...

Xem khóa học
★★★★★ 5 ★ 1 👤 0 ▥ 0
Quảng cáo

Bạn nên đọc

Leave a Reply

Your email address will not be published. Required fields are marked *

Quảng cáo

Cũ vẫn chất

Xem thêm