gpt4 book ai didi

vba - Outlook 2010宏引发VBA错误 'For loop not initialized'

转载 作者:行者123 更新时间:2023-12-03 09:08:39 25 4
gpt4 key购买 nike

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

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块仍会导致错误。

最佳答案

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

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更宽容,并跳过了该条目。

关于vba - Outlook 2010宏引发VBA错误 'For loop not initialized',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5544361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com