首页 > 解决方案 > 如何在多个帐户上运行规则?

问题描述

我之前询问过如何让规则适用于多个帐户。已部分回答,但规则不适用于第二个帐户。

运行脚本时会弹出进度窗口,但不运行任何规则。

规则的调试输出列出了帐户的所有规则。如果我手动运行规则,它确实有效。

决定将宏一分为二。运行第一个宏适用于第一个帐户。但是第二个帐户的第二个宏不起作用。

我的假设是它试图在第一个帐户上运行这些规则,但没有成功,因为这些规则不存在。

Sub RunTest_AccountOne()

Dim storeRules As Rules
Dim storeRule As Rule
Dim allStores As Stores
Dim myStore As Store
Dim emailaccount As String


Set allStores = Session.Stores
emailaccount = "example@example.com.au"

For Each myStore In allStores
    
    'On Error Resume Next
    'If needed, place just before the expected error.
    'Follow closely with On Error GoTo 0 to return to normal error handling.
    
    'Debug.Print myStore.DisplayName & "  " & myStore.ExchangeStoreType
    
    Select Case myStore.DisplayName
    
        Case emailaccount
            'Debug.Print " ExchangeStoreType matched: " _
              & myStore.DisplayName & "  " & myStore.ExchangeStoreType
            
            Set storeRules = myStore.GetRules()
            For Each storeRule In storeRules
                'Below only works if it is the first account added to Outlook.
                'storeRule.Execute 'ShowProgress:=False
                
                'Below shows the progress window for rules running, but doesn't apply anything.
                storeRule.Execute ShowProgress:=True, Folder:=myStore.GetDefaultFolder(olFolderInbox), RuleExecuteOption:=olRuleExecuteAllMessages
                
                'Below outputs a bunch of "Inbox" messages in the console, which coincides with the amount of rules, as expected.
                'Debug.Print myStore.GetDefaultFolder(olFolderInbox)
                
                
            Next
    
    End Select
    
Next

End Sub

我试过RuleExecuteOption:=olRuleExecuteAllMessages有无。

标签: vbaoutlook

解决方案


推荐阅读