Outlook 2010 - Run a rule based on schedule

Posted on 2011-10-26
Last Modified: 2012-05-12
Hello all.

I am using outlook 2010 and have made a rule for a specific purpose. I can easily turn this rule on and off manually.

What I am looking for is an automatic, unattended way to turn this rule on and off. I know I can use a VB script to turn the rule on and off and I can also use windows task scheduler to take care of the scheduling. But, I have not found a script (yet) that turns on/off a SPECIFIC rule - only scripts that turn on/off ALL rules (which is not what I need).

This rule would need to be turned on during off business hours (5pm - 8am) and all day during weekend days.

Anyone know how to do this?

Please only respond if your solution answers my query exactly :)
Question by:sshani
    LVL 3

    Accepted Solution

    Hi sshani,

    There is one ugly way to do this, which is by creating a rule that runs a VBA script.

    The VBA script can check the day and time and decide whether to proceed or not.

    However, you need to configure Outlook to run macros at all times, you can do this by selecting File -> Options -> Trust Center -> Trust Center Settings -> Macro Settings -> Enable All Macros, then you need to restart Outlook for the changes to take effect.

    Assuming that your week days are Sunday to Thursday and off working hours are 5pm to 8am, and assuming that you want to move all messages received during these times to a folder called "My Folder" then the script shown in the attachment can be used.

    Sub MoveMeIfNecessary(Item As Outlook.MailItem)
        On Error Resume Next
        Dim wd As Integer
        Dim h As Integer
        'This rule is enabled during off business hours (5pm - 8am) and all day during weekend days (Sunday and Monday)
        wd = Weekday(Date)  'week day: Sunday = 1, Monday = 2, ...
        h = hour(Time)      'Hour: 24-Hour format (0-23)
        If (wd = 1 Or wd = 2 Or h > 17 Or h < 8) Then
            Item.Move GetFolderByName("My Folder")
        End If
    End Sub
    ' The below code is taken from:
    ' GetFolderByName
    ' By: Jeff Rockow
    ' Purpose: Search the folder tree to get any folder, based on
    '          its name alone
    ' Arguments: strFolderName = name of folder to search for
    ' Returns: MAPIFolder object if folder exists and is unique,
    '          otherwise Nothing
    '          intFolderCount is also accessible
    Public Function GetFolderByName(strFolderName As String, Optional objFolder As Outlook.MAPIFolder, Optional intFolderCount) As MAPIFolder
    Dim objApp As Outlook.Application
    Dim objNS As Outlook.NameSpace
    Dim colStores As Outlook.Folders
    Dim objStore As Outlook.MAPIFolder
    Dim colFolders As Outlook.Folders
    Dim objResult As Outlook.MAPIFolder
    Dim I As Long
    On Error Resume Next
      Set objApp = CreateObject("Outlook.Application")
      Set objNS = objApp.GetNamespace("MAPI")
      Set colStores = objNS.Folders
      If objFolder Is Nothing Then
        'If objFolder is not passed, assume this is the initial call and cycle through stores
        intFolderCount = 0
        For Each objStore In colStores
          Set objResult = GetFolderByName(strFolderName, objStore, intFolderCount)
          If Not objResult Is Nothing Then Set GetFolderByName = objResult
        'Test to see if this folder's name matches the search criteria
        If objFolder.Name = strFolderName Then
          Set GetFolderByName = objFolder
          intFolderCount = intFolderCount + 1
        End If
        Set colFolders = objFolder.Folders
        'Cycle through the sub folders with recursive calls to this function
        For Each objFolder In colFolders
          Set objResult = GetFolderByName(strFolderName, objFolder, intFolderCount)
          If Not objResult Is Nothing Then Set GetFolderByName = objResult
      End If
      'If two or more folders exist with the same name, set the function to Nothing
      If intFolderCount > 1 Then Set GetFolderByName = Nothing
      Set objResult = Nothing
      Set colFolders = Nothing
      Set objNS = Nothing
      Set objApp = Nothing
    End Function

    Open in new window

    To insert this script into Outlook you need to press Alt+F11 to open Microsoft Visual Basic for Application editor and then copy/paste the code below under ThisOutlookSession or under a new module that you create for this purpose.

    The rule that you will be creating will need to run the function MoveMeIfNecessary
    Outlook Rules Wizard

    Author Comment

    Almost what I need. I can use the date/time check in the script, but I don't really need anything moved. I really need emails forwarded. I already have a rule to forward incoming emails. Is there a way to change the script you included to simply run the other rule I have already created?
    LVL 3

    Expert Comment

    You can modify the script I attached to forward the message for you, for example you can use:
    ' This code is taken from here:
    Set myForward = Item.Forward
    myForward.Recipients.Add ""

    Open in new window

    If you you want to run an existing Outlook rule from the script, then you can modify the code as follows:
    ' This code is a modified version of the code in:
    ' Get default store (where rules live)
    Set st = Application.Session.DefaultStore
    ' Get rules
    Set myRules = st.GetRules
    ' Find the required rule by its name
    ' The code below assumes the rule name is: Forward Me
    For Each rl In myRules
            If rl.Name = "Forward Me" Then
                rl.Execute ShowProgress:=True
                Exit For
            End If

    Open in new window


    Author Closing Comment

    I figured it out. I used a derivation of what you had posted. I would not have been able to do it without your listing - so you get full points.  Thanks!

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Learn more about how the humble email signature can be used as more than just an electronic business card. When used correctly, a signature can easily be tailored for different purposes by different departments within an organization.
    Find out how to use dynamic social media in email signatures with this top 10 DOs & DON’Ts.
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now