共用方式為


建立規則,根據主旨中的多個字組,將類別指派給郵件專案

此範例示範如何設定規則,根據主旨中的多個字組,將類別指派給郵件專案。

範例

注意事項

下列程式代碼範例是 Microsoft Office Outlook 2007 程式設計應用程式的摘錄。

在 Outlook 中,可以將專案分類為更容易組織和顯示。 Outlook 物件模型提供 Category 物件和 Categories 集合來代表類別。 如需 Category 物件和 Outlook 專案之 Categories 集合的詳細資訊,請參閱 列舉和新增類別

以 Rule 物件表示的規則可以指派多個條件。 您可以取得或設定陣列,代表要評估的條件或要完成的動作。 例如,TextRuleCondition 物件的 Text 屬性會傳回或設定字串專案的陣列,表示規則條件要評估的文字。 您必須指派具有一個字串或多個字串的陣列以進行評估。 若要評估陣列中指派的多個文字字串,請使用邏輯 OR 運算。

您可以用來取得或設定陣列的屬性如下:Address、Categories、CategoriesFormNameTextRuleCondition.Text。 如需規則的詳細資訊,請 參閱從管理員建立檔案郵件項目的規則和為它們加上旗標以進行後續追蹤

在下列範例中,CreateTextAndCategoryRule 會使用 CategoryExists 方法,依 Categories 集合 中的名稱 “Office” 或 “Outlook” 來檢查使用者的郵件專案是否有任何類別。 如果找不到任何類別,則會新增它們。 然後,此範例會建立包含 「Office、」Outlook“ 和 「2007」 的字串陣列。 此陣列將代表要評估的條件。 CreateTextAndCategoryRule 接著會使用 TextRuleCondition 物件的 Text 屬性和 RuleConditions 集合的 BodyOrSubject 屬性,藉由檢查數位中任何條件的主旨,來建立指派類別的規則。 如果符合條件,則會使用 RuleActions 物件的 AssignToCategory 方法,將 Office 和 Outlook 的類別指派給專案。

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; }
}

另請參閱