共用方式為


立即執行規則

這個範例示範如何使用 Rule 物件的 Execute (Object、Object、Object、Object) 方法,立即執行 規則

範例

注意事項

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

您可以在 Rule 物件上呼叫 Execute 方法,讓規則立即執行。 Execute 方法的參數是選擇性的;如果未指定,則規則會套用至 [收件匣] 中的所有訊息,但不會套用至 [收件匣] 的子資料夾,並使用參數的預設值。 下表列出 Execute 方法之選擇性參數的預設值。

參數

預設值

ShowProgress

資料夾

收件匣

IncludeSubfolders

RuleExecuteOption

OlRuleExecuteOption.olRuleExecuteAllMessages

您可以使用規則和警示精靈取消規則執行。 您也可以將 ShowProgress 參數設定為 true ,然後取消進度對話框,以取消規則執行。 取消進度對話框之後, Execute 會傳回錯誤。

在下列程式代碼範例中,ExecuteManagerRule 會取得 CreateManagerRule 程式中所建立的規則,該程式來自從管理員建立 規則至檔案郵件專案,以及將它們標幟為待處理。 ExecuteManagerRule 接著會檢查規則是否不是 Null 參考。 如果規則不是 Null 參考,ExecuteManagerRule 會使用預設參數在規則上呼叫 Execute 方法,立即執行規則。

注意事項

若要套用規則一次,不論 Enabled 屬性是否傳回 true,請使用 Rule.Execute 方法。 若要套用目前會話和目前會話以外的規則,請使用 Rule.Enabled 屬性和 Save (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 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);
        }
    }
}

另請參閱