Outlook 2010 宏引发 VBA 错误“For 循环未初始化”

发布于 2024-10-29 08:25:43 字数 1231 浏览 3 评论 0原文

几年前我从某人的博客中得到了这段代码。它基本上遍历所有 Outlook 邮件规则并执行它们(方便组织您的收件箱!)。我最近从 2007 年升级到 Outlook 2010。现在我收到一个非常奇怪的错误,指出

Run-time error '92':
For loop not initialized

然而,在调试此问题时,它总是会运行 8 次(20-25 次),然后抛出此错误。

以下是有问题的代码:

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

    'get default store (where rules live) & get rules
    Set st = Application.Session.DefaultStore
    Set myRules = st.GetRules

    'iterate all the rules
    For Each rl In myRules
        If rl.RuleType = olRuleReceive Then         'determine if it’s an Inbox rule, 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

编辑:

根据 Jay Riggs 的评论,清除整个 for 块仍然会导致错误。

I got this bit of code from someone's blog years ago. It basically iterates through all the Outlook mail rules, and executes them (handy to organize your inbox!). I've recently upgrade to Outlook 2010 from 2007. Now I get a very strange error stating

Run-time error '92':
For loop not initialized

However, while debugging this, it will always run through 8 times (out of 20-25), then it throw this error.

Here is the offending code:

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

    'get default store (where rules live) & get rules
    Set st = Application.Session.DefaultStore
    Set myRules = st.GetRules

    'iterate all the rules
    For Each rl In myRules
        If rl.RuleType = olRuleReceive Then         'determine if it’s an Inbox rule, 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

Edit:

Per Jay Riggs's comment, clearing the entire for block still results in the error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飞烟轻若梦 2024-11-05 08:25:43

我会将此循环替换为以下内容:

Dim k as Long
For k = 1 To myRules.Count ' might be 0-based, didnt check
    set rl = myRules(k)
    If rl.RuleType = olRuleReceive Then         'determine if it’s an Inbox rule, if so, run it
        rl.Execute ShowProgress:=True
        count = count + 1
        ruleList = ruleList & vbCrLf & rl.Name
    End If
Next

我打赌位置 8 或 9 处有一些规则不适合 myRules 集合,并且会引发错误。您还可以检查有问题的点的 myRules 集合。也许 Office 2007 更宽容并跳过了该条目。

I'd replace this loop with something like:

Dim k as Long
For k = 1 To myRules.Count ' might be 0-based, didnt check
    set rl = myRules(k)
    If rl.RuleType = olRuleReceive Then         'determine if it’s an Inbox rule, if so, run it
        rl.Execute ShowProgress:=True
        count = count + 1
        ruleList = ruleList & vbCrLf & rl.Name
    End If
Next

I bet there is some rule at position 8 or 9 that doesn't fit into the myRules collection and that throws the error. You can also check the myRules collection at the offending point. Maybe Office 2007 was more forgiving and skipped that entry.

屋顶上的小猫咪 2024-11-05 08:25:43

所以问题最终是一些规则引用了我在移动到新机器时错过的 PST 文件。感谢贾斯汀迫使我更深入地了解规则!

+1 给这个晦涩的错误消息。

So the issue ended up being that some of the rules referred to a PST file I missed on moving to my new machine. Thanks to Justin for forcing me to take a deeper look at the rules!

+1 to the obscure error message for this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文