• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 15162
  • Last Modified:

Outlook 2010 - Run a rule based on schedule

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?
Regards.

Please only respond if your solution answers my query exactly :)
0
sshani
Asked:
sshani
  • 2
  • 2
1 Solution
 
komarCommented:
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")
        Item.Save
    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
    Next
  Else
    '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
    Next
  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
0
 
sshaniAuthor Commented:
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?
0
 
komarCommented:
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"
myForward.Send

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
Next

Open in new window

0
 
sshaniAuthor Commented:
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!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now