Create a rule to assign categories to mail items based on multiple words in the subject

This example shows how to set up a rule that assigns categories to mail items based on multiple words in the subject.

Example

Note

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

In Outlook, items can be categorized for easier organization and display. The Outlook object model provides the Category object and the Categories collection to represent categories. For more information about the Category object and the Categories collection for an Outlook item, see Enumerate and Add Categories.

A rule, represented by a Rule object, can be assigned with multiple conditions. You can get or set an array that represents conditions to be evaluated or actions to be completed. For example, the Text property of the TextRuleCondition object returns or sets an array of string elements that represents the text to be evaluated by the rule condition. You must assign an array with one string or multiple strings for evaluation. To evaluate multiple text strings that are assigned in an array, use the logical OR operation.

The properties that you can use to get or set an array are as follows: Address, Categories, Categories, FormName, and TextRuleCondition.Text. For more information about rules, see Create a Rule to File Mail Items from a Manager and Flag Them for Follow-Up.

In the following example, CreateTextAndCategoryRule uses the CategoryExists method to check the user’s mail items for any categories by the name “Office” or “Outlook” in the Categories collection. If no categories are found, they are added. The example then creates an array of strings that include “Office, “Outlook”, and “2007”. This array will represent the conditions to be evaluated. CreateTextAndCategoryRule then creates a rule that assigns categories by examining the subject for any of the conditions in the array by using the Text property of the TextRuleCondition object and the BodyOrSubject property of the RuleConditions collection. If the condition is satisfied, the categories of Office and Outlook are assigned to the item by using the AssignToCategory method of the RuleActions object.

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 CreateTextAndCategoryRule()
{
    if (!CategoryExists("Office"))
    {
        Application.Session.Categories.Add(
            "Office", Type.Missing, Type.Missing);
    }
    if (!CategoryExists("Outlook"))
    {
        Application.Session.Categories.Add(
            "Outlook", Type.Missing, Type.Missing);
    }
    Outlook.Rules rules =
        Application.Session.DefaultStore.GetRules();
    Outlook.Rule textRule =
        rules.Create("Demo Text and Category Rule",
        Outlook.OlRuleType.olRuleReceive);
    Object[] textCondition = 
        { "Office", "Outlook", "2007" };
    Object[] categoryAction = 
        { "Office", "Outlook" };
    textRule.Conditions.BodyOrSubject.Text =
        textCondition;
    textRule.Conditions.BodyOrSubject.Enabled = true;
    textRule.Actions.AssignToCategory.Categories =
        categoryAction;
    textRule.Actions.AssignToCategory.Enabled = true;
    rules.Save(true);
}

// Determines if categoryName exists in Categories collection
private bool CategoryExists(string categoryName)
{
    try
    {
        Outlook.Category category =
            Application.Session.Categories[categoryName];
        if (category != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    catch { return false; }
}

See also