Outlook 2010宏引发VBA错误';对于未初始化的循环';
几年前我从某人的博客上得到了这段代码。它基本上遍历所有Outlook邮件规则,并执行它们(便于组织您的收件箱!)。我最近从2007年升级到了Outlook 2010。现在我得到一个非常奇怪的错误Outlook 2010宏引发VBA错误';对于未初始化的循环';,vba,outlook,runtime-error,Vba,Outlook,Runtime Error,几年前我从某人的博客上得到了这段代码。它基本上遍历所有Outlook邮件规则,并执行它们(便于组织您的收件箱!)。我最近从2007年升级到了Outlook 2010。现在我得到一个非常奇怪的错误 Run-time error '92': For loop not initialized 但是,在调试时,它将始终运行8次(共20-25次),然后抛出此错误 以下是违规代码: Sub RunAllInboxRules() Dim st As Outlook.Store Dim my
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更宽容一些,跳过了这个条目。我想用以下内容替换这个循环:
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
集合。也许Office2007更宽容一些,跳过了这个条目。所以问题是,一些规则涉及到我在移动到新机器时丢失的PST文件。感谢贾斯汀迫使我更深入地了解规则
+因此,问题最终是一些规则引用了我在移动到新机器时丢失的PST文件。感谢贾斯汀迫使我更深入地了解规则
+因为它总是只运行8次,所以第9条规则有什么特别之处吗?因为它总是只运行8次,所以第9条规则有什么特别之处吗?