[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Outlook 2010 - Run a rule based on schedule

Posted on 2011-10-26
Medium Priority
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
  • 2
  • 2

Accepted Solution

komar earned 750 total points
ID: 37033821
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: http://www.outlookcode.com/codedetail.aspx?id=492
' 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

ID: 37034153
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?

Expert Comment

ID: 37034377
You can modify the script I attached to forward the message for you, for example you can use:
' This code is taken from here: http://support.microsoft.com/kb/292063
Set myForward = Item.Forward
myForward.Recipients.Add "myaddress@mydomain.com"

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: http://www.outlookcode.com/codedetail.aspx?id=1266
' 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

ID: 37050770
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Mailbox Corruption is a nightmare every Exchange DBA wishes he never has. Recovering from it can be super-hectic if not entirely futile. And though techniques like the New-MailboxRepairRequest cmdlet have been designed to help with fixing minor corr…
MS Outlook undoubtedly is the most widely used email client.Its user-friendliness, cost effectiveness, and availability with Microsoft Office Suite make it the most popular email application.  Its compatibility with Microsoft applications like Exch…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
Many of my clients call in with monstrous Gmail overloading issues with Outlook. A quick tip is to turn off the All Mail and Important folders from synching. Here is a quick video I made to show you how to turn off these and other folders in Gmail s…
Suggested Courses

834 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