Outlook 2016 VBA to Run Rules automatically on schedule











up vote
0
down vote

favorite












Trying to run a simple rule to move emails upon arrival from first public email box to second public email box (Rule is: Apply this rule after the message arrives | Sent to "first public email box" | move it to the "second public email box" folder). Rule works when run manually but the rule does not work automatically upon emails arriving (research shows there might be some corrupt file, blah blah). Trying to make it work via VBA instead. Below macro supposed to run the rules upon a reminder popping up. Reminder pops up, but rules don't run. Running that macro from a QAT custom button, brings up progress window, and that window shows progress, but emails are still in the first public email box.



Private Sub Application_Reminder(ByVal Item As Object)

If Item.MessageClass <> "IPM.Task" Then
Exit Sub
End If

If Item.Subject = "Run Rules" Then
RunRules
End If

End Sub

Sub RunRules()

Dim olRules As Outlook.Rules
Dim myRule As Outlook.Rule
Dim olRuleNames() As Variant
Dim name As Variant

olRuleNames = Array("Rule A", "Rule B")

Set olRules = Application.Session.DefaultStore.GetRules()

For Each name In olRuleNames()
For Each myRule In olRules
' Rules we want to run
If myRule.name = name Then
myRule.Execute ShowProgress:=True
End If
Next
Next
End Sub









share|improve this question


























    up vote
    0
    down vote

    favorite












    Trying to run a simple rule to move emails upon arrival from first public email box to second public email box (Rule is: Apply this rule after the message arrives | Sent to "first public email box" | move it to the "second public email box" folder). Rule works when run manually but the rule does not work automatically upon emails arriving (research shows there might be some corrupt file, blah blah). Trying to make it work via VBA instead. Below macro supposed to run the rules upon a reminder popping up. Reminder pops up, but rules don't run. Running that macro from a QAT custom button, brings up progress window, and that window shows progress, but emails are still in the first public email box.



    Private Sub Application_Reminder(ByVal Item As Object)

    If Item.MessageClass <> "IPM.Task" Then
    Exit Sub
    End If

    If Item.Subject = "Run Rules" Then
    RunRules
    End If

    End Sub

    Sub RunRules()

    Dim olRules As Outlook.Rules
    Dim myRule As Outlook.Rule
    Dim olRuleNames() As Variant
    Dim name As Variant

    olRuleNames = Array("Rule A", "Rule B")

    Set olRules = Application.Session.DefaultStore.GetRules()

    For Each name In olRuleNames()
    For Each myRule In olRules
    ' Rules we want to run
    If myRule.name = name Then
    myRule.Execute ShowProgress:=True
    End If
    Next
    Next
    End Sub









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Trying to run a simple rule to move emails upon arrival from first public email box to second public email box (Rule is: Apply this rule after the message arrives | Sent to "first public email box" | move it to the "second public email box" folder). Rule works when run manually but the rule does not work automatically upon emails arriving (research shows there might be some corrupt file, blah blah). Trying to make it work via VBA instead. Below macro supposed to run the rules upon a reminder popping up. Reminder pops up, but rules don't run. Running that macro from a QAT custom button, brings up progress window, and that window shows progress, but emails are still in the first public email box.



      Private Sub Application_Reminder(ByVal Item As Object)

      If Item.MessageClass <> "IPM.Task" Then
      Exit Sub
      End If

      If Item.Subject = "Run Rules" Then
      RunRules
      End If

      End Sub

      Sub RunRules()

      Dim olRules As Outlook.Rules
      Dim myRule As Outlook.Rule
      Dim olRuleNames() As Variant
      Dim name As Variant

      olRuleNames = Array("Rule A", "Rule B")

      Set olRules = Application.Session.DefaultStore.GetRules()

      For Each name In olRuleNames()
      For Each myRule In olRules
      ' Rules we want to run
      If myRule.name = name Then
      myRule.Execute ShowProgress:=True
      End If
      Next
      Next
      End Sub









      share|improve this question













      Trying to run a simple rule to move emails upon arrival from first public email box to second public email box (Rule is: Apply this rule after the message arrives | Sent to "first public email box" | move it to the "second public email box" folder). Rule works when run manually but the rule does not work automatically upon emails arriving (research shows there might be some corrupt file, blah blah). Trying to make it work via VBA instead. Below macro supposed to run the rules upon a reminder popping up. Reminder pops up, but rules don't run. Running that macro from a QAT custom button, brings up progress window, and that window shows progress, but emails are still in the first public email box.



      Private Sub Application_Reminder(ByVal Item As Object)

      If Item.MessageClass <> "IPM.Task" Then
      Exit Sub
      End If

      If Item.Subject = "Run Rules" Then
      RunRules
      End If

      End Sub

      Sub RunRules()

      Dim olRules As Outlook.Rules
      Dim myRule As Outlook.Rule
      Dim olRuleNames() As Variant
      Dim name As Variant

      olRuleNames = Array("Rule A", "Rule B")

      Set olRules = Application.Session.DefaultStore.GetRules()

      For Each name In olRuleNames()
      For Each myRule In olRules
      ' Rules we want to run
      If myRule.name = name Then
      myRule.Execute ShowProgress:=True
      End If
      Next
      Next
      End Sub






      vba outlook






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 12:59









      Alex

      218




      218
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          The use of DefaultStore is a hint there are stores other than the default.



          Set olRules = Application.Session.DefaultStore.GetRules()



          Option Explicit

          Private Sub FindStoreWithRules()

          Dim colStores As stores
          Dim oStore As store

          Dim olRules As rules
          Dim myRule As Rule

          Dim i As Long

          Set colStores = Session.stores

          For i = 1 To colStores.count

          Debug.Print i & ": " & colStores(i)

          ' On second run
          ' Enter applicable name then uncomment this line and the End If
          ' If colStores(i) = "Name of store shown in immediate window" Then

          On Error Resume Next
          ' Where rules not applicable on some stores there is an error.
          Set olRules = colStores(i).GetRules()
          ' Discontinue error bypass as soon as the purpose is served
          On Error GoTo 0

          If Not olRules Is Nothing Then
          For Each myRule In olRules
          ' Uncomment on second run to see if what rules were found
          'Debug.Print " - " & myRule.name
          Next
          Else
          Debug.Print "Rules not applicable in " & colStores(i)
          End If

          ' End If

          Next

          Debug.Print "Done."

          End Sub





          share|improve this answer





















          • On the first run in Immediate window it printed 11 mailbox names, one is mine and 10 Public. Under one that I am trying to automate running the rules it printed "Rules not applicable in Public Folders - ABC@domain.com" On line: Debug.Print i & ": " & colStores(i) – it gave “Run-time error’-2147024891 (80070005)’: You do not have sufficient permission to perform this operation on this object. See the folder contact or your system administrator.” Am I to interpret all that as what I am trying to accomplish is not possible?
            – Alex
            Nov 9 at 18:16










          • I cannot comment on your permissions. You will have to investigate how you are set up in Outlook. One thing I try to mention on code that fails for shared "mailboxes". Try adding an account if you have only a mailbox michev.info/Blog/Post/1507
            – niton
            Nov 9 at 19:06











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226169%2foutlook-2016-vba-to-run-rules-automatically-on-schedule%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          The use of DefaultStore is a hint there are stores other than the default.



          Set olRules = Application.Session.DefaultStore.GetRules()



          Option Explicit

          Private Sub FindStoreWithRules()

          Dim colStores As stores
          Dim oStore As store

          Dim olRules As rules
          Dim myRule As Rule

          Dim i As Long

          Set colStores = Session.stores

          For i = 1 To colStores.count

          Debug.Print i & ": " & colStores(i)

          ' On second run
          ' Enter applicable name then uncomment this line and the End If
          ' If colStores(i) = "Name of store shown in immediate window" Then

          On Error Resume Next
          ' Where rules not applicable on some stores there is an error.
          Set olRules = colStores(i).GetRules()
          ' Discontinue error bypass as soon as the purpose is served
          On Error GoTo 0

          If Not olRules Is Nothing Then
          For Each myRule In olRules
          ' Uncomment on second run to see if what rules were found
          'Debug.Print " - " & myRule.name
          Next
          Else
          Debug.Print "Rules not applicable in " & colStores(i)
          End If

          ' End If

          Next

          Debug.Print "Done."

          End Sub





          share|improve this answer





















          • On the first run in Immediate window it printed 11 mailbox names, one is mine and 10 Public. Under one that I am trying to automate running the rules it printed "Rules not applicable in Public Folders - ABC@domain.com" On line: Debug.Print i & ": " & colStores(i) – it gave “Run-time error’-2147024891 (80070005)’: You do not have sufficient permission to perform this operation on this object. See the folder contact or your system administrator.” Am I to interpret all that as what I am trying to accomplish is not possible?
            – Alex
            Nov 9 at 18:16










          • I cannot comment on your permissions. You will have to investigate how you are set up in Outlook. One thing I try to mention on code that fails for shared "mailboxes". Try adding an account if you have only a mailbox michev.info/Blog/Post/1507
            – niton
            Nov 9 at 19:06















          up vote
          0
          down vote













          The use of DefaultStore is a hint there are stores other than the default.



          Set olRules = Application.Session.DefaultStore.GetRules()



          Option Explicit

          Private Sub FindStoreWithRules()

          Dim colStores As stores
          Dim oStore As store

          Dim olRules As rules
          Dim myRule As Rule

          Dim i As Long

          Set colStores = Session.stores

          For i = 1 To colStores.count

          Debug.Print i & ": " & colStores(i)

          ' On second run
          ' Enter applicable name then uncomment this line and the End If
          ' If colStores(i) = "Name of store shown in immediate window" Then

          On Error Resume Next
          ' Where rules not applicable on some stores there is an error.
          Set olRules = colStores(i).GetRules()
          ' Discontinue error bypass as soon as the purpose is served
          On Error GoTo 0

          If Not olRules Is Nothing Then
          For Each myRule In olRules
          ' Uncomment on second run to see if what rules were found
          'Debug.Print " - " & myRule.name
          Next
          Else
          Debug.Print "Rules not applicable in " & colStores(i)
          End If

          ' End If

          Next

          Debug.Print "Done."

          End Sub





          share|improve this answer





















          • On the first run in Immediate window it printed 11 mailbox names, one is mine and 10 Public. Under one that I am trying to automate running the rules it printed "Rules not applicable in Public Folders - ABC@domain.com" On line: Debug.Print i & ": " & colStores(i) – it gave “Run-time error’-2147024891 (80070005)’: You do not have sufficient permission to perform this operation on this object. See the folder contact or your system administrator.” Am I to interpret all that as what I am trying to accomplish is not possible?
            – Alex
            Nov 9 at 18:16










          • I cannot comment on your permissions. You will have to investigate how you are set up in Outlook. One thing I try to mention on code that fails for shared "mailboxes". Try adding an account if you have only a mailbox michev.info/Blog/Post/1507
            – niton
            Nov 9 at 19:06













          up vote
          0
          down vote










          up vote
          0
          down vote









          The use of DefaultStore is a hint there are stores other than the default.



          Set olRules = Application.Session.DefaultStore.GetRules()



          Option Explicit

          Private Sub FindStoreWithRules()

          Dim colStores As stores
          Dim oStore As store

          Dim olRules As rules
          Dim myRule As Rule

          Dim i As Long

          Set colStores = Session.stores

          For i = 1 To colStores.count

          Debug.Print i & ": " & colStores(i)

          ' On second run
          ' Enter applicable name then uncomment this line and the End If
          ' If colStores(i) = "Name of store shown in immediate window" Then

          On Error Resume Next
          ' Where rules not applicable on some stores there is an error.
          Set olRules = colStores(i).GetRules()
          ' Discontinue error bypass as soon as the purpose is served
          On Error GoTo 0

          If Not olRules Is Nothing Then
          For Each myRule In olRules
          ' Uncomment on second run to see if what rules were found
          'Debug.Print " - " & myRule.name
          Next
          Else
          Debug.Print "Rules not applicable in " & colStores(i)
          End If

          ' End If

          Next

          Debug.Print "Done."

          End Sub





          share|improve this answer












          The use of DefaultStore is a hint there are stores other than the default.



          Set olRules = Application.Session.DefaultStore.GetRules()



          Option Explicit

          Private Sub FindStoreWithRules()

          Dim colStores As stores
          Dim oStore As store

          Dim olRules As rules
          Dim myRule As Rule

          Dim i As Long

          Set colStores = Session.stores

          For i = 1 To colStores.count

          Debug.Print i & ": " & colStores(i)

          ' On second run
          ' Enter applicable name then uncomment this line and the End If
          ' If colStores(i) = "Name of store shown in immediate window" Then

          On Error Resume Next
          ' Where rules not applicable on some stores there is an error.
          Set olRules = colStores(i).GetRules()
          ' Discontinue error bypass as soon as the purpose is served
          On Error GoTo 0

          If Not olRules Is Nothing Then
          For Each myRule In olRules
          ' Uncomment on second run to see if what rules were found
          'Debug.Print " - " & myRule.name
          Next
          Else
          Debug.Print "Rules not applicable in " & colStores(i)
          End If

          ' End If

          Next

          Debug.Print "Done."

          End Sub






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 9 at 16:53









          niton

          5,38171941




          5,38171941












          • On the first run in Immediate window it printed 11 mailbox names, one is mine and 10 Public. Under one that I am trying to automate running the rules it printed "Rules not applicable in Public Folders - ABC@domain.com" On line: Debug.Print i & ": " & colStores(i) – it gave “Run-time error’-2147024891 (80070005)’: You do not have sufficient permission to perform this operation on this object. See the folder contact or your system administrator.” Am I to interpret all that as what I am trying to accomplish is not possible?
            – Alex
            Nov 9 at 18:16










          • I cannot comment on your permissions. You will have to investigate how you are set up in Outlook. One thing I try to mention on code that fails for shared "mailboxes". Try adding an account if you have only a mailbox michev.info/Blog/Post/1507
            – niton
            Nov 9 at 19:06


















          • On the first run in Immediate window it printed 11 mailbox names, one is mine and 10 Public. Under one that I am trying to automate running the rules it printed "Rules not applicable in Public Folders - ABC@domain.com" On line: Debug.Print i & ": " & colStores(i) – it gave “Run-time error’-2147024891 (80070005)’: You do not have sufficient permission to perform this operation on this object. See the folder contact or your system administrator.” Am I to interpret all that as what I am trying to accomplish is not possible?
            – Alex
            Nov 9 at 18:16










          • I cannot comment on your permissions. You will have to investigate how you are set up in Outlook. One thing I try to mention on code that fails for shared "mailboxes". Try adding an account if you have only a mailbox michev.info/Blog/Post/1507
            – niton
            Nov 9 at 19:06
















          On the first run in Immediate window it printed 11 mailbox names, one is mine and 10 Public. Under one that I am trying to automate running the rules it printed "Rules not applicable in Public Folders - ABC@domain.com" On line: Debug.Print i & ": " & colStores(i) – it gave “Run-time error’-2147024891 (80070005)’: You do not have sufficient permission to perform this operation on this object. See the folder contact or your system administrator.” Am I to interpret all that as what I am trying to accomplish is not possible?
          – Alex
          Nov 9 at 18:16




          On the first run in Immediate window it printed 11 mailbox names, one is mine and 10 Public. Under one that I am trying to automate running the rules it printed "Rules not applicable in Public Folders - ABC@domain.com" On line: Debug.Print i & ": " & colStores(i) – it gave “Run-time error’-2147024891 (80070005)’: You do not have sufficient permission to perform this operation on this object. See the folder contact or your system administrator.” Am I to interpret all that as what I am trying to accomplish is not possible?
          – Alex
          Nov 9 at 18:16












          I cannot comment on your permissions. You will have to investigate how you are set up in Outlook. One thing I try to mention on code that fails for shared "mailboxes". Try adding an account if you have only a mailbox michev.info/Blog/Post/1507
          – niton
          Nov 9 at 19:06




          I cannot comment on your permissions. You will have to investigate how you are set up in Outlook. One thing I try to mention on code that fails for shared "mailboxes". Try adding an account if you have only a mailbox michev.info/Blog/Post/1507
          – niton
          Nov 9 at 19:06


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226169%2foutlook-2016-vba-to-run-rules-automatically-on-schedule%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Schultheiß

          Verwaltungsgliederung Dänemarks

          Liste der Kulturdenkmale in Wilsdruff