Home > Blockchain >  Outlook rule run in all the folders
Outlook rule run in all the folders

Time:04-15

I have a VBA script that is running a rule in outlook repeating the rule execution every 10 minutes. Is working like a charm, but I have a small problem, the rule is working just in the Inbox folder, and I need to work on all the folders. I tried with "IncludeSubfolders:=True" without luck. Here is my script:

Public Sub TriggerTimer(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idevent As Long, ByVal Systime As Long)
Dim oOk As Outlook.Application

Set oOk = GetObject(, "Outlook.Application")
If Err.Number <> 0 Then Set oOk = CreateObject("Outlook.Application")
On Error GoTo 0

oOk.Session.DefaultStore.GetRules.Item("AssistantPlanifRobot").Execute

MsgBox "Rule run"
End Sub

and the timer part:

Public Sub ActivateTimer(ByVal nMinutes As Long)
  nMinutes = nMinutes * 1000 * 60 'milliseconds
  If TimerID <> 0 Then Call DeactivateTimer 'Check to see if timer is running before call to SetTimer
  TimerID = SetTimer(0, 0, nMinutes, AddressOf TriggerTimer)
  If TimerID = 0 Then
    MsgBox "The timer failed to activate."
  End If
End Sub

Does anyone have any idea?

Thank you :)

CodePudding user response:

As stated above, the default behavior of running rules is to be run in Inbox.

But you can run a specific rule on a specific folder. I manually moved some mails making the rule object, in a specific folder and run the next code, which works:

Sub testDefaultStore()
   Dim oRules As Outlook.Rules, rul As Outlook.Rule, ruleName As String
   Dim objNS As NameSpace, ctrlBr As Outlook.Folder, folderName As String
   
   ruleName = "the real rule name" 
   folderName ="the folder name where to run"' 
   Set objNS = Application.GetNamespace("MAPI")
   Set ctrlBr = Application.Session.GetDefaultFolder(olFolderInbox).Folders(folderName)
   
   Set oRules = Application.Session.DefaultStore.GetRules
   For Each rul In oRules
        Debug.Print rul.RuleType, rul.Name
        If rul.Name = ruleName Then
            rul.Execute ShowProgress:=True, Folder:=ctrlBr
        End If
   Next
End Sub

It is just an example of running on a specific folder.

Knowing the rule name and folder name, you can directly try:

oOk.Session.DefaultStore.GetRules.Item("AssistantPlanifRobot").Execute ShowProgress:=True, Folder:=ctrlBr

The used folder should be set as above. Even if it is not an Inbox subfolder, I can show you how, but now I must leave my office.

For Inbox and its subfolders, it should work as:

oOk.Session.DefaultStore.GetRules.Item("AssistantPlanifRobot").Execute ShowProgress:=True,IncludeSubfolders:=True

CodePudding user response:

By default, rules are run against the Inbox folder in Outlook.

Instead of relying on the rules in Outlook (end-user things) you can do everything in the code, so consider implementing the required functionality in VBA without rules involved.

  • Related