Reputation: 37
Why is this code to run all enabled rules does not run in outlook-2016? I used it without any problem on outlook 2007, 2010 and 2013, but it is not working in outlook 2016.
Sub OrginalRunActiveCatInboxRules()
Dim Session As Outlook.NameSpace
Dim Report As String
Dim currentItem As Object
Dim currentRule As Outlook.Rule
Dim rules As Outlook.rules
Dim ruleList As String
Set Session = Application.Session
Set rules = Session.DefaultStore.GetRules()
For Each currentRule In rules
If currentRule.RuleType = olRuleReceive Then
'determine if it’s an Inbox rule, if so, run it
If currentRule.Enabled Then
If Left(currentRule.name, 4) = "Cat." Then
MsgBox currentRule.name
currentRule.Execute ShowProgress:=True
' Sortering niet laten zien
' ruleList = ruleList & vbCrLf & currentRule.Name
End If
End If
End If
Next
' tell the user what you did
' ruleList = "These rules were executed against the Inbox: " & vbCrLf & ruleList
' MsgBox ruleList, vbInformation, "Macro: RunAllActiveInboxRules"
Set rules = Nothing
Set currentRule = Nothing
End Sub
The codes runs until next, but gives a error there. It does not matter if the first rule is enabled or not, starts with Cat. or not. It just won't go to the next item in the loop, but generates a error.
What can I do to make the code working again?
Upvotes: 0
Views: 950
Reputation: 1
I recently ran into this same issue when writing a macro in Outlook 365 to run some rules. I don't know why, but as J. Farro mentioned in their answer, certain conditions being enabled on any rule seems to cause the run-time error '-2146664191 (800c8101)' when trying to enumerate the Rules object.
In my case the "through the specified account" condition on one of my rules was the culprit. Removing that condition stopped the error. If you aren't able to remove the condition, you can use a different approach to index the Rules object as mentioned in this Microsoft article:
You can retrieve each rule in a Rules collection by indexing the collection using Rules.Item(Index), with Index being either the name of the rule (the default property Rule.Name), or a value ranging from 1 through the total number of rules in the collection, Rules.Count.
You can use this approach with error handling disabled to skip the rule(s) with troublesome conditions that can't be accessed through VBA
Sub PrintAllRules()
Dim Rules As Outlook.Rules
Dim Rule As Outlook.Rule
Set Rules = Application.Session.DefaultStore.GetRules
For i = 1 To Rules.Count
'if the Rules.Item method below fails, Rule will retain it's value
'from the previous loop iteration so we need to clear it each time
Set Rule = Nothing
On Error Resume Next
Set Rule = Rules.Item(i)
On Error GoTo 0
If Rule Is Nothing Then
Debug.Print "Rule " & i & " could not be accessed"
Else
Debug.Print Rule.Name
End If
Next i
End Sub
Unfortunately, you will still not be able to access the rule(s) with the troublesome condition, but those rules won't break your code and prevent you from accessing all your other rules.
Upvotes: 0
Reputation: 49
One of mine was marked "on this computer" when I unselected this option, the script worked again.
Upvotes: -1
Reputation: 71
Check in the rules wizard if some of the rules are marked 'From other computer'. If you have any like this, then the code will break at the line:
For Each currentRule In rules
I have the same issue now - took me a lot of troubleshooting to figure this out. One more thing that could be an issue: if any of the rules are broken, for example refer to a directory that does not exist, the rules object will not contain any rules.
If troubleshooting the above, remove all the suspicious rules and start adding them one by one back.
Upvotes: 1