共用方式為


在本機電腦上執行規則

此範例示範如何使用 RuleConditions 物件的 OnLocalMachine 屬性,在本機電腦上執行規則。

範例

注意事項

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

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

對於預設在用戶端上執行的規則動作, (例如顯示新的郵件警示) ,則會自動啟用 OnLocalMachine 條件,而且針對僅限用戶端的 RuleAction 物件,Enabled 屬性會設定為 true。 對於通常在伺服器上執行的規則動作,請設定僅限用戶端 RuleAction 物件的 Enabled 屬性,以啟用 OnLocalMachine 條件。 這會強制規則在用戶端本機上執行。

啟用規則 的 OnLocalMachine 條件時,當從另一部電腦檢查相同的規則時,也會啟用 OnOtherMachine 條件。 olConditionOtherMachine 類型的規則條件表示規則只能在目前電腦以外的特定電腦上執行,而且無法以程式設計方式啟用或停用。 例如,如果在目前的計算機上建立規則,而且已啟用 OnLocalMachine 規則條件,則規則只能在該電腦上執行。 如果在另一部計算機上執行相同的規則,規則會顯示已啟用 OnOtherMachine 規則條件。

在下列程式代碼範例中,DemoOnMachineOnly 會建立規則,並藉由將 Enabled 屬性設定為 true 來啟OnlyToMe 條件和轉寄動作。 接著會啟用 OnLocalMachine 條件,強制伺服器端規則在本機執行,並儲存規則。 根據預設, 轉寄 動作和 OnlyToMe 條件會在伺服器上運作。 啟用 OnLocalMachine 條件之後,它們會以客戶端規則運作。

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

另請參閱