0

I previously asked about getting rules to work on multiple accounts. It was partially answered, however rules do not work on the second account.

When running the script the progress window pops up, but no rules run.

Debug output of the rules lists all the rules for the account. If I manually run the rules, it does work.

Decided to split the macro into two. Running the first macro works for the first account. But the second macro for the second account doesn't work.

My assumption is it is trying to run those rules on the first account, but not succeeding because those rules do not exist.

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

I have tried with RuleExecuteOption:=olRuleExecuteAllMessages and without.

Community
  • 1
  • 1
  • Would you confirm you have a dropdown "Apply changes to this folder:" in the E-mail Rules tab of the Rules and Alerts dialog. https://www.sperrysoftware.com/Email-Tools/wp-content/uploads/2019/03/New-Customer-Order-WooCommerce-Rule.png – niton Feb 19 '21 at 14:21
  • As I said I can run the rules manually. But I will confirm for you :) – Crazyaussie21 Feb 19 '21 at 20:47
  • Yes, I do have that and it is set to the right email address. – Crazyaussie21 Feb 21 '21 at 23:05
  • There is no apparent problem with the code. A common cause of restricted functionality is the mailbox not being an account. If you are not sure, see if you have a separate account for that email address https://stackoverflow.com/a/62731358/1571407. If not this describes how to add an account https://support.microsoft.com/en-us/office/add-an-email-account-to-outlook-6e27792a-9267-4aa4-8bb6-c84ef146101b – niton Feb 21 '21 at 23:43
  • I have added all the accounts as individual accounts. As per the output. – Crazyaussie21 Feb 21 '21 at 23:58
  • As discussed earlier, if I have the show progress enabled it shows the little window as if rules are running but nothing moves. Unsure why – Crazyaussie21 Feb 21 '21 at 23:59
  • Given no other suggestions, you could reproduce what you want in `ItemAdd` code. https://stackoverflow.com/questions/11263483/how-do-i-trigger-a-macro-to-run-after-a-new-mail-is-received-in-outlook – niton Feb 22 '21 at 17:03
  • Will take a look when I get in the office today mate. Thanks – Crazyaussie21 Feb 22 '21 at 20:55

0 Answers0