Search code examples
vbaoutlook

How to run rules on accounts with specified ExchangeStoreType?


I am setting up automation for multiple email accounts that my team monitors.

I created my own rule for an individual account. I have done some googling and editing to get it to work for multiple accounts.

It is listing accounts I do not want.

I want to set variables for all the outputs, so I can then run rules on specific accounts.

Sub RunTest()
Dim storeRules As Outlook.Rules
Dim storeRule As Outlook.Rule
Dim allStores As Outlook.Stores
Dim myStore As Outlook.Store

Set allStores = Application.Session.Stores
For Each myStore In allStores
    On Error Resume Next
    Debug.Print myStore.DisplayName & "  " & myStore.ExchangeStoreType
    Set storeRules = myStore.GetRules()
    For Each storeRule In storeRules
        'storeRule.Execute ShowProgress:=True' 'disabled until code is working'
    Next
Next
End Sub

The output of the Debug Log is: (**** covers sensitive information)

Public Folders - ****@****.com.au  2
****  1
****  1
**** ****  1
**** **** ****  1
**** ****  1
****@****.com.au  4
****  1
****@****.com.au  0
Public Folders - ****@****.com.au  2

I want to run rules for 4 and 0. How do I set variables for individual items in what is effectively an array?


Solution

  • Apply the myStore.ExchangeStoreType conditions.

    Option Explicit
    
    Sub RunTest_ExchangeStoreType()
    
    Dim storeRules As Rules
    Dim storeRule As Rule
    Dim allStores As Stores
    Dim myStore As Store
    
    Set allStores = Session.Stores
    
    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.ExchangeStoreType
        
            Case 0, 4
                Debug.Print " ExchangeStoreType matched: " _
                  & myStore.DisplayName & "  " & myStore.ExchangeStoreType
                
                Set storeRules = myStore.GetRules()
                For Each storeRule In storeRules
                    'storeRule.Execute ShowProgress:=True' 'disabled until code is working'
                Next
        
        End Select
        
    Next
    
    End Sub