Execute a rule instantly

This example shows how to execute a rule instantly by using the Execute(Object, Object, Object, Object) method of the Rule object.

Example

Note

The following code example is an excerpt from Programming Applications for Microsoft Office Outlook 2007.

You can cause a rule to execute immediately by calling the Execute method on the Rule object. The parameters to the Execute method are optional; if they are not specified, the rule will be applied to all messages in the Inbox but not to the subfolders of the Inbox, and default values for the parameters will be used. The following table lists the default values for the optional parameters of the Execute method.

Parameter

Default value

ShowProgress

False

Folder

Inbox

IncludeSubfolders

False

RuleExecuteOption

OlRuleExecuteOption.olRuleExecuteAllMessages

You can cancel a rule execution by using the Rules and Alerts Wizard. You can also cancel a rule execution by setting the ShowProgress parameter to true and then canceling the progress dialog box. Once you cancel the progress dialog box, Execute will return an error.

In the following code example, ExecuteManagerRule gets the rule that was created in the procedure CreateManagerRule from the topic Create a Rule to File Mail Items from a Manager and Flag Them for Follow-Up. ExecuteManagerRule then checks whether the rule is not a null reference. If the rule is not a null reference, ExecuteManagerRule calls the Execute method on the rule with default parameters, instantly executing the rule.

Note

To apply a rule once, regardless of whether the Enabled property returns true, use the Rule.Execute method. To apply the rule for the current session and beyond the current session, use both the Rule.Enabled property and the Save(Object) method.

If you use Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 15.0 Object Library component and specify the Outlook variable when you import the Microsoft.Office.Interop.Outlook namespace. The using statement must not occur directly before the functions in the code example but must be added before the public Class declaration. The following line of code shows how to do the import and assignment in C#.

using Outlook = Microsoft.Office.Interop.Outlook;
private void ExecuteManagerRule()
{
    Outlook.AddressEntry currentUser =
        Application.Session.CurrentUser.AddressEntry;
    if (currentUser.Type == "EX")
    {
        try
        {
            string managerName = currentUser.
                GetExchangeUser().GetExchangeUserManager().Name;
            Outlook.Rule managerRule =
                Application.Session.DefaultStore.GetRules()[managerName];
            if (managerRule != null)
            {
                managerRule.Execute(false, Type.Missing,
                    Type.Missing, Type.Missing);
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
        }
    }
}

See also