Home > Blockchain >  When Walking The Rules Collection How Do I Determine If A Rule Has An Error And What The Error Is?
When Walking The Rules Collection How Do I Determine If A Rule Has An Error And What The Error Is?

Time:01-29

Outlook Version 2211 (Build 15831.20208 Click-to-Run) Access Version 2211 (Build 15831.20208 Click-to-Run)

Set olApp = GetObject(, "Outlook.Application") 
'   Get the Outlook objects we need 
Set olNameSpace = olApp.GetNamespace("MAPI") 
Set olFolder = olNameSpace.GetDefaultFolder(olFolderInbox) 
Set olStore = olFolder.Store 
Set olRules = olStore.GetRules 
lRecordNumberEnd = olRules.Count 
        For Each olRule In olRules 

***** Is there a way to determine if a rule has been classified as in error from the olRule object and possibly what the error is?

If I am processing a rule that moves a message to a specific folder, I will get an error when I try to reference the folder name:

  Case olRuleActionMoveToFolder '   Rule action is to move the message to the specified folder." 
       Set olMoveOrCopyRuleAction = olAction 
       On Error Resume Next 
           rs.Fields("Information").Value = "     " & "     " & olMoveOrCopyRuleAction.Folder 
           If Err.Number <> 0 Then 
               If Err.Number = 91 Then 
                    rs.Fields("Information").Value = "     " & "     " & "***** Folder not defined *****" 
                Else 
                     rs.Fields("Information").Value = "     " & "     " & Err.Description 
                End If 
                rs.Fields("Status").Value = " **Error**" 
            End If 
       On Error GoTo 0 

Now if you are in Outlook and click on Rule/Manage Rules and Alerts, Outlook will display a single popup:

One or more rules contain errors. Delete or modify these rules.

So, Outlook has a simple way to determine that at least one enabled rule is in contains an error. If the rule that has an error is disabled, Outlook still highlights it in the list of rules.

So, my question is:
How do I identify that a rule has an error and what the error is without having to process the rule and throw an error?

CodePudding user response:

There is no generic way to do that, but you can do a sanity check on at least the rules that point to a folder, such as copy or move action rule - check Rule.Actions.MoveToFolder / CopyToFolder actions are enabled, try to access MoveOrCopyRuleAction.Folder property and see if it raises an error.

CodePudding user response:

You can use the Rules.Save method do diagnose if anything is wrong with a rules. Saving rules that are incompatible or have improperly defined actions or conditions (such as an empty string for TextRuleCondition.Text or wrong folder used) will return an error.

Rules are not fully exposed in the object model. You can use VBA to create the rules, but not repair them. For example, see Create a Rule to Move Specific Emails to a Folder.

  • Related