共用方式為


建立規則以從管理員提出郵件專案,並將它們標示為待處理

此範例示範如何設定規則,以從使用者的管理員提出郵件專案,併為它們加上旗標以進行後續追蹤。

範例

注意事項

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

Outlook 規則可以根據帳戶和規則的類型,在伺服器端或客戶端運作。 當您在信箱中組織專案時,有許多方式可以實作規則來強制執行您自己的組織配置。 例如,您可以建立子資料夾階層,以組織未讀取的郵件,並依主旨區域讀取郵件。 或者,您可以建立對應至訊息傳送者的子資料夾階層。 您也可以將郵件分類,然後使用搜尋資料夾依類別匯總郵件。

Rules 物件模型包含代表 Outlook 中規則的 Rule 物件,可讓您以程式設計方式建立規則,以強制執行特定組織配置、建立解決方案唯一的特定規則,或確定特定規則已部署至一組使用者。 您可以使用 Rules 物件模型,以程式設計方式新增、編輯和刪除規則。 藉由使用 Rules 集合和 Rule 物件,您也可以存取、新增和刪除針對會話定義的規則。

Rule 物件具有 RuleType 屬性,指出規則是傳送或接收規則。 建立規則時, 會指定 RuleType 屬性,而且若沒有刪除規則,並使用不同的 RuleType 屬性重新建立規則,就無法變更。 RuleActionRuleCondition 物件、其集合物件,以及衍生的動作和條件物件也可用來進一步支援編輯規則動作和規則條件。

Rules 物件模型不支援您可以在 Outlook 使用者介面中使用規則和警示精靈建立的所有規則,但它支援最常使用的規則動作和條件。 任何使用 [規則和警示精靈] 建立的規則,套用至郵件,包括郵件專案、會議要求、工作要求、文件、傳遞收據、讀取收據、投票回應和辦公室外通知,也可以透過程序設計方式建立。

規則可以在 Exchange 伺服器或 Outlook 用戶端上執行,前提是目前使用者的信箱裝載於 Exchange 伺服器上。 Rule 物件的 IsLocalRule 屬性會傳回 true,表示規則會在用戶端上執行,而 Outlook 必須正在執行,規則才能執行。 如果規則在伺服器上執行,則 Outlook 不需要執行,即可評估規則條件和要完成的規則動作。

注意事項

沒有代表規則例外狀況的個別集合。 使用 Rule 物件的 Exceptions 屬性可取得代表規則例外狀況的 RuleConditions 集合。

若要透過 Outlook 物件模型建立規則,請遵循下列步驟:

  1. 由呼叫默Store 物件上的 GetRules () 方法,從 NameSpace 物件的 DefaultStore 屬性取得 Rules 集合。 使用 試用...catch 區塊,以考慮使用者已離線或中斷與 Exchange 伺服器的連線。 這可防止 Outlook 引發錯誤。

  2. Rules 物件上呼叫 Create (String、OlRuleType) 方法,以建立實例變數或 Rule 物件,並指定 Name 和 OlRuleType 參數。

  3. 使用 RuleActionsRuleConditions 集合來啟用 Rule 物件上的動作、條件和例外狀況。 請注意,由 Exceptions 屬性傳回之 RuleConditions 集合中啟用的任何條件都會被視為規則例外狀況,而且無法將額外的內建自定義動作或條件新增至集合。

  4. 將 Enabled 屬性設定為 true,讓任何指定的規則動作、條件或例外狀況正常運作。 某些動作或條件,例如 Folder 屬性,會要求您在動作或條件上設定其他屬性,以儲存 Rule 物件,而不會發生錯誤。

  5. 最後,在 Rules 集合上呼叫 Save (Object) 方法,以儲存已建立或修改的規則。 將 Save 方法括在 嘗試中...catch 區塊來處理例外狀況。

在下列程式代碼範例中,CreateManagerRule 會實作先前所述的步驟。 CreateManagerRule 會先驗證 CurrentUser 屬性是否代表 ExchangeUser 物件,指出目前的使用者是 Exchange 使用者。 如果目前的使用者是 Exchange 使用者,CreateManagerRule 會在 NameSpace 物件之 CurrentUser 屬性的 ExchangeUser 物件上呼叫 GetExchangeUserManager () 方法,以取得目前使用者的管理員。 接著會建立接收規則,將接收到的訊息移至收件匣的子資料夾,以符合下列條件:

  • 訊息來自使用者的管理員。

  • 收件者位於郵件的 [ 收件者: ] 行上。

  • 訊息不是會議邀請或更新。

最後,訊息會標示為要在今天進行後續追蹤。 CreateManagerRule 也會說明可能引發例外狀況的適當錯誤處理,例如使用者在快取的 Exchange 模式中離機或中斷連線。

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 CreateManagerRule()
{
    Outlook.ExchangeUser manager;
    Outlook.Folder managerFolder;
    Outlook.AddressEntry currentUser =
        Application.Session.CurrentUser.AddressEntry;
    if (currentUser.Type == "EX")
    {
        try
        {
            manager = currentUser.
                GetExchangeUser().GetExchangeUserManager();
        }
        catch
        {
            Debug.WriteLine("Could not obtain user's manager.");
            return;
        }
        Outlook.Rules rules;
        try
        {
            rules = Application.Session.DefaultStore.GetRules();
        }
        catch
        {
            Debug.WriteLine("Could not obtain rules collection.");
            return;
        }
        if (manager != null)
        {
            string displayName = manager.Name;
            Outlook.Folders folders =
                Application.Session.GetDefaultFolder(
                Outlook.OlDefaultFolders.olFolderInbox).Folders;
            try
            {
                managerFolder =
                    folders[displayName] as Outlook.Folder;
            }
            catch
            {
                managerFolder =
                    folders.Add(displayName, Type.Missing)
                    as Outlook.Folder;
            }
            Outlook.Rule rule = rules.Create(displayName,
                Outlook.OlRuleType.olRuleReceive);

            // Rule conditions
            // From condition
            rule.Conditions.From.Recipients.Add(
                manager.PrimarySmtpAddress);
            rule.Conditions.From.Recipients.ResolveAll();
            rule.Conditions.From.Enabled = true;

            // Sent only to me
            rule.Conditions.ToMe.Enabled = true;

            // Rule exceptions
            // Meeting invite or update
            rule.Exceptions.MeetingInviteOrUpdate.Enabled = true;

            // Rule actions
            // MarkAsTask action
            rule.Actions.MarkAsTask.MarkInterval =
                Outlook.OlMarkInterval.olMarkToday;
            rule.Actions.MarkAsTask.FlagTo = "Follow-up";
            rule.Actions.MarkAsTask.Enabled = true;

            // MoveToFolder action
            rule.Actions.MoveToFolder.Folder = managerFolder;
            rule.Actions.MoveToFolder.Enabled = true;
            try
            {
                rules.Save(true);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
    }
}

另請參閱