none
VSTO : Outlook 2007 rules help OnLocalMachine RRS feed

  • Question

  • Hi,

    I am unable to create a rule in Outlook that applies to any machine, I would really like some help on how to do this.

    I am trying to make a rule that copies all sent emails to another existing folder.

    I have included the code below, with the problem area. The frustrating thing is I can go into Outlook if I skip these two lines and remove the condition Apply to local machine from within the rules wizard, so I know it can be done, I just can't seem to get it done in code though, any help appreciated.

    Outlook.Store iStore = this.OutlookApp.Session.DefaultStore;

            Outlook.Rules iRules = iStore.GetRules();
            Outlook.Rule iRule = iRules.Create("Test", Outlook.OlRuleType.olRuleSend);

            Outlook.MoveOrCopyRuleAction mcAction = iRule.Actions.CopyToFolder;

            mcAction.Enabled = true;
            mcAction.Folder = emailFolder; // instantiated elsewhere, omitted for clarity

            // Errors on this section ***********
            Outlook.RuleCondition rcAction = iRule.Conditions.OnLocalMachine;
            rcAction.Enabled = false;
            // ***********************************

            iRule.Enabled = true;
            iRule.ExecutionOrder = 1;

            iRules.Save(false);

    Now I've read the documentation which says OnLocalMachine depending on the rule maybe set, but I can go into the rules wizard in Outlook and create such a rule that applies to all machines. Please help if there is some other property or mechanism that I should use, many thanks.

    regards,

    Russell

    Sunday, May 27, 2012 12:32 PM

Answers

  • I'm not quite sure where any error would be occurring or why. I translated your code into VBA for a quick test in the Outlook VBA project, and it worked with no errors with and without the lines you say cause an error. In both cases the OnLocalMachine setting ended up false with or without those code lines.
     
    Try the following Outlook VBA code in the Outlook VBA project and see if you get errors both with and without the lines about OnLocalMachine:
     
    Sub RulesTest1()
        Dim iStore As Outlook.Store
        Set iStore = Application.Session.DefaultStore
        Dim iRules As Outlook.Rules
        Set iRules = iStore.GetRules()
        Dim iRule As Outlook.Rule
        Set iRule = iRules.Create("Test", Outlook.OlRuleType.olRuleSend)
        Dim mcAction As Outlook.MoveOrCopyRuleAction
        Set mcAction = iRule.Actions.CopyToFolder
        mcAction.Enabled = True
        mcAction.Folder = Application.Session.GetDefaultFolder(olFolderDrafts) 'emailFolder ' // instantiated elsewhere, omitted for clarity
       
        '// Errors on this section ***********
        'Dim rcAction As Outlook.RuleCondition
        'Set rcAction = iRule.Conditions.OnLocalMachine
        'rcAction.Enabled = False
        '// ***********************************
       
        iRule.Enabled = True
        iRule.ExecutionOrder = 1
       
        iRules.Save (False)
    End Sub

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "TiamatVenger" <=?utf-8?B?VGlhbWF0VmVuZ2Vy?=> wrote in message news:c7ca5fe4-e99f-4d8c-85d4-54320103157f...

    Hi,

    I am unable to create a rule in Outlook that applies to any machine, I would really like some help on how to do this.

    I am trying to make a rule that copies all sent emails to another existing folder.

    I have included the code below, with the problem area. The frustrating thing is I can go into Outlook if I skip these two lines and remove the condition Apply to local machine from within the rules wizard, so I know it can be done, I just can't seem to get it done in code though, any help appreciated.

    Outlook.Store iStore = this.OutlookApp.Session.DefaultStore;

            Outlook.Rules iRules = iStore.GetRules();
            Outlook.Rule iRule = iRules.Create("Test", Outlook.OlRuleType.olRuleSend);

            Outlook.MoveOrCopyRuleAction mcAction = iRule.Actions.CopyToFolder;

            mcAction.Enabled = true;
            mcAction.Folder = emailFolder; // instantiated elsewhere, omitted for clarity

            // Errors on this section ***********
            Outlook.RuleCondition rcAction = iRule.Conditions.OnLocalMachine;
            rcAction.Enabled = false;
            // ***********************************

            iRule.Enabled = true;
            iRule.ExecutionOrder = 1;

            iRules.Save(false);

    Now I've read the documentation which says OnLocalMachine depending on the rule maybe set, but I can go into the rules wizard in Outlook and create such a rule that applies to all machines. Please help if there is some other property or mechanism that I should use, many thanks.

    regards,

    Russell


    Ken Slovak MVP - Outlook
    Tuesday, May 29, 2012 1:41 PM

All replies

  • Hi TiamatVenger,

    What did the compiler report about the 2 lines of code?

    regards,

    Tuesday, May 29, 2012 10:32 AM
  • I'm not quite sure where any error would be occurring or why. I translated your code into VBA for a quick test in the Outlook VBA project, and it worked with no errors with and without the lines you say cause an error. In both cases the OnLocalMachine setting ended up false with or without those code lines.
     
    Try the following Outlook VBA code in the Outlook VBA project and see if you get errors both with and without the lines about OnLocalMachine:
     
    Sub RulesTest1()
        Dim iStore As Outlook.Store
        Set iStore = Application.Session.DefaultStore
        Dim iRules As Outlook.Rules
        Set iRules = iStore.GetRules()
        Dim iRule As Outlook.Rule
        Set iRule = iRules.Create("Test", Outlook.OlRuleType.olRuleSend)
        Dim mcAction As Outlook.MoveOrCopyRuleAction
        Set mcAction = iRule.Actions.CopyToFolder
        mcAction.Enabled = True
        mcAction.Folder = Application.Session.GetDefaultFolder(olFolderDrafts) 'emailFolder ' // instantiated elsewhere, omitted for clarity
       
        '// Errors on this section ***********
        'Dim rcAction As Outlook.RuleCondition
        'Set rcAction = iRule.Conditions.OnLocalMachine
        'rcAction.Enabled = False
        '// ***********************************
       
        iRule.Enabled = True
        iRule.ExecutionOrder = 1
       
        iRules.Save (False)
    End Sub

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "TiamatVenger" <=?utf-8?B?VGlhbWF0VmVuZ2Vy?=> wrote in message news:c7ca5fe4-e99f-4d8c-85d4-54320103157f...

    Hi,

    I am unable to create a rule in Outlook that applies to any machine, I would really like some help on how to do this.

    I am trying to make a rule that copies all sent emails to another existing folder.

    I have included the code below, with the problem area. The frustrating thing is I can go into Outlook if I skip these two lines and remove the condition Apply to local machine from within the rules wizard, so I know it can be done, I just can't seem to get it done in code though, any help appreciated.

    Outlook.Store iStore = this.OutlookApp.Session.DefaultStore;

            Outlook.Rules iRules = iStore.GetRules();
            Outlook.Rule iRule = iRules.Create("Test", Outlook.OlRuleType.olRuleSend);

            Outlook.MoveOrCopyRuleAction mcAction = iRule.Actions.CopyToFolder;

            mcAction.Enabled = true;
            mcAction.Folder = emailFolder; // instantiated elsewhere, omitted for clarity

            // Errors on this section ***********
            Outlook.RuleCondition rcAction = iRule.Conditions.OnLocalMachine;
            rcAction.Enabled = false;
            // ***********************************

            iRule.Enabled = true;
            iRule.ExecutionOrder = 1;

            iRules.Save(false);

    Now I've read the documentation which says OnLocalMachine depending on the rule maybe set, but I can go into the rules wizard in Outlook and create such a rule that applies to all machines. Please help if there is some other property or mechanism that I should use, many thanks.

    regards,

    Russell


    Ken Slovak MVP - Outlook
    Tuesday, May 29, 2012 1:41 PM