2011-04-04 64 views
2

我從幾年前的某人的博客得到了這一點的代碼。它基本上遍歷所有的Outlook郵件規則,並執行它們(方便地組織您的收件箱!)。我最近升級到Outlook 2010從2007年現在,我得到一個非常奇怪的錯誤,說明Outlook 2010宏引發VBA錯誤'For循環未初始化'

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 

編輯:

每週杰倫裏格斯的評論,清除塊整個仍然導致錯誤。

+1

由於它總是隻運行8次,第9條規則還有什麼特別之處嗎? – Justin 2011-04-05 15:25:54

回答

0

所以這個問題最終導致一些規則提到了我錯過搬到我的新機器上的PST文件。感謝賈斯汀強迫我深入瞭解規則!

+1爲此隱藏的錯誤消息。

0

我會取代這個循環的東西,如:

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更寬容並跳過了該條目。