Outlook 2010 宏引发 VBA 错误“For 循环未初始化”
几年前我从某人的博客中得到了这段代码。它基本上遍历所有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会将此循环替换为以下内容:
我打赌位置 8 或 9 处有一些规则不适合
myRules
集合,并且会引发错误。您还可以检查有问题的点的myRules
集合。也许 Office 2007 更宽容并跳过了该条目。I'd replace this loop with something like:
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 themyRules
collection at the offending point. Maybe Office 2007 was more forgiving and skipped that entry.所以问题最终是一些规则引用了我在移动到新机器时错过的 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.