Execute a rule on a local computer

This example shows how to execute a rule on a local computer by using the OnLocalMachine property of the RuleConditions object.

Example

Note

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

If your mailbox is hosted on an Exchange server, a rule can be executed on the Exchange server or on the Outlook client. If the rule is executed on the server, Outlook does not have to be running for the rule conditions to be evaluated and the rule actions to be completed. If the rule is executed on the client, Outlook must be running for the rule to execute. If the IsLocalRule property of the Rule object returns true, the rule is executed on the client.

For rule actions that execute on the client by default (such as displaying a new mail alert), the OnLocalMachine condition will automatically be enabled, and the Enabled property is set to true for a client-side-only RuleAction object. For rule actions that generally execute on the server, set the Enabled property of the client-side-only RuleAction object to enable the OnLocalMachine condition. This will force the rule to execute locally on the client.

When the OnLocalMachine condition for a rule is enabled, the OnOtherMachine condition will also be enabled when the same rule is examined from another machine. A rule condition of type olConditionOtherMachine indicates that the rule can execute only on a specific computer other than the current one, and cannot be programmatically enabled or disabled. For example, if a rule is created on the current computer and the OnLocalMachine rule condition is enabled, the rule can execute only on that computer. If the same rule is executed on another computer, the rule will show that the OnOtherMachine rule condition is enabled.

In the following code example, DemoOnMachineOnly creates a rule and enables the OnlyToMe condition and Forward action by setting the Enabled properties to true. The OnLocalMachine condition is then enabled, forcing a server-side rule to execute locally, and the rules are saved. By default, a Forward action and OnlyToMe condition will operate on the server. Once the OnLocalMachine condition has been enabled, they will operate as a client-side rule.

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 DemoOnMachineOnly()
{
    Outlook.Rules rules =
        Application.Session.DefaultStore.GetRules();
    Outlook.Rule rule =
        rules.Create("Demo Machine Only Rule",
        Outlook.OlRuleType.olRuleReceive);
    rule.Conditions.OnlyToMe.Enabled = true;
    rule.Actions.Forward.Enabled = true;
    rule.Actions.Forward.Recipients.Add("someone@example.com");
    rule.Actions.Forward.Recipients.ResolveAll();

    // Force the rule to execute locally
    rule.Conditions.OnLocalMachine.Enabled = true;
    rules.Save(true);
}

See also