Outlook - 运行所有规则

问题描述 投票:0回答:1

我希望通过一个按钮运行所有 Outlookrules,并从另一个站点获取以下代码,但它会生成错误: 运行时错误-2146664191 (800c8101) 自动化错误

我在 Outlook 中有 22 条规则,但代码只会运行第一个规则,该规则恰好名为“MKG”。如果我不注释掉“On error resume Next”行,它当然会运行,但只运行第一个规则“MKG”。 代码在 NEXT 处失败并出现自动化错误。 “对于 myRules 中的每个 rl”似乎是正确的,但如果没有自动化错误,NEXT 将不会触发”消息。 任何人都可以建议进行更改以使运行完全运行吗?

    Sub RunAllInboxRules()
    Dim st As Outlook.Store
    Dim myRules As Outlook.Rules
    Dim rl As Outlook.Rule
    Dim count As Integer
    Dim ruleList As String
    
    On Error Resume Next
        
    ' get default store (where rules live)
    Set st = Application.Session.DefaultStore
    
    ' get rules
    Set myRules = st.GetRules
    
        ' iterate all the rules
    For Each rl In myRules
        ' determine if it's an Inbox rule;
            If rl.RuleType = olRuleReceive And rl.IsLocalRule = True Then
         'if so run it
            rl.Execute ShowProgress:=True
            count = count + 1
            ruleList = ruleList & vbCrLf & rl.name
        End If
      Next

    ' tell the user what you did
    ruleList = "These rules were executed against the Inbox: " & vbCrLf & ruleList
    MsgBox ruleList, vbInformation, "Macro: RunAllInboxRules"

    Set rl = Nothing
    Set st = Nothing
    Set myRules = Nothing
    End Sub

我已经搜索了有关该问题的帮助,但没有找到任何可以解决该问题的内容。 自从发布这个问题以来,我在我妻子的电脑上放置了相同的宏,它运行完美,没有遇到任何错误。两台电脑都在 Windows 10Pro 下运行 Outlook 2019 桌面版。这让我相信我的电脑某个地方有丢失或损坏的文件。但是我的电脑运行另一个 Outlook 宏没有问题。常见的问题似乎是任何带有循环的问题,例如 For every > NEXT 或 Do Until 或 Do While > LOOP,即使是带有 NEXT 或 LOOP 的非常简单的宏也会导致“自动化错误 = For 循环未初始化”。关于哪些文件可能丢失或损坏有什么想法吗?

automation outlook rules
1个回答
0
投票

在找到合理的解释之前,请尝试索引循环。

Option Explicit

Sub RunAllInboxRulesIndexedLoop()

    ' base code
    ' https://www.slipstick.com/outlook/rules/run-outlook-rules-startup/
    
    Dim st As Store
    Dim myRules As Rules
    Dim rl As Rule
    
    Dim count As Long
    Dim ruleList As String
    
    ' get default store (where rules live)
    Set st = Session.defaultStore
    
    ' get rules
    Set myRules = st.GetRules
    
    ' iterate rules with an indexed loop
    Dim i As Long
    For i = 1 To myRules.count
    
        ' determine if it's an Inbox rule
        Set rl = myRules(i)
        Debug.Print "rl.Name: " & rl.Name
        Debug.Print " rl.RuleType: " & rl.RuleType
        
        If rl.RuleType = olRuleReceive Then
        
            Debug.Print " rl.IsLocalRule: " & rl.IsLocalRule
            If rl.IsLocalRule = True Then
                'if so run it
                rl.Execute ShowProgress:=True
                count = count + 1
                Debug.Print " count: " & count & " of " & i
                ruleList = ruleList & vbCrLf & rl.Name
            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: RunAllInboxRules"

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.