none
Problem with Filter property on Outlook's AutoFormattingRule object (2) RRS feed

  • Question

  • NOTE: This thread is a continuation of the following thread (which broke -- not possible to post to it anymore):

    https://social.msdn.microsoft.com/Forums/office/en-US/881da0dc-40ff-4d81-9920-429a74035472/problem-with-filter-property-on-outlooks-autoformattingrule-object?forum=outlookdev

    Hi Eugene,

    My project is using Javascript. 

    I tried the VBA routine you proposed and find that it reproduces the problem I am having.

    After I executed the routine, initially no items in my folder are formatted/underlined. If I check the settings of the view, I can see the custom conditional formatting rule that was added by the script, but I can also see that the filter/condition looks blank. If I navigate away from the folder and back, the conditional formatting is applied to ALL items (all items become underlined), which indicates 1) that it is necessary to perform some additional action for the rule to take effect, and 2) that the filter is not working. 

    Do you see a different behavior when running the routine in your Outlook?

    Thanks,

    Mats

    PS. Sorry for starting a new thread for this issue, but the page of the original thread is broken -- some javascript error prevents me from posting replies on that page.

    Tuesday, April 19, 2016 10:26 AM

Answers

  • Hi Mats,

    >> I'm having problems with programmatic creation (e.g. VBA) of conditional formatting rules in folders/views in Outlook utizling this API:

    Do you develop with VBA/C# or javascript? Based on your example code, it seems you are using javascript, could you share us which project do you develop?

    >> the Filter property is not properly set it seems, with the result that the rule is applied to all items in the folder, not just the ones selected by the filter expression.

    I suggest you check View->Current View-> View Settings->Conditional Formatting-> check rules for your view->Search for the word after you run your code, and check whether the value set correctly. If it is empty, the rule will be applied to all items.

    Here is a simple code for VBA:

    Sub FilterView()
    
    Dim v As Outlook.TableView
    
    Dim r As Outlook.AutoFormatRule
    
    Set v =   Application.ActiveExplorer.CurrentFolder.CurrentView
    
    Set r = v.AutoFormatRules.Add("test")
    
    r.Font.Underline = True
    
    r.Filter =   """http://schemas.microsoft.com/mapi/proptag/0x0037001f"""   + " LIKE '%thread%'"
    
    r.Enabled = True
    
    v.AutoFormatRules.Save
    
    v.Save
    
    End Sub
    

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, April 19, 2016 5:43 AM

All replies

  • Hi,

    I'm having problems with programmatic creation (e.g. VBA) of conditional formatting rules in folders/views in Outlook utizling this API:

    https://msdn.microsoft.com/en-us/library/office/ff866587.aspx

    When adding a new AutoFormatRule to a view programmatically, the Filter property is not properly set it seems, with the result that the rule is applied to all items in the folder, not just the ones selected by the filter expression.

    For example:

    var view = application.activeExplorer.curentFolder.currentView;
    var rule = view.autoFormatRules.add("Underline tests");
    rule.font.underline = true;
    rule.filter = "\"urn:schemas:httpmail:subject\" LIKE '%test%'";
    rule.enabled = true;
    view.autoFormatRules.save()
    view.save();

    Effects:

    • All items underlined, not just the ones with word "test" in subject.
    • In the Conditional Formatting dialog in the Outlook UI, the Filter dialog looks empty.
    • In the debugging console, if I call view.autoFormatRules("Underline tests").filter after the view.save() statement in the script above, the error message "Invalid procedure call or argument" is printed to the console.

    Grateful for any clarification!

    Thanks,

    Mats

    Monday, April 18, 2016 11:02 AM
  • Hello Matsew,

    Looks like you need to correct the Filter property value:

    string filter = "@SQL=" 
        + "\"" + "urn:schemas:httpmail:subject" + "\"" 
      + " like '%test%'";

    See Chapter 11: Searching Outlook Data for more information.

    Also I'd suggest using the Apply method right after calling the Save method of the View class.


    [custom.development]

    Monday, April 18, 2016 12:24 PM
  • Hi Eugene,

    Thanks for the suggestion. I will try it out and report back.

    By the way, I am wondering if there is an example published somewhere how to set auto formatting rules.

    Thanks,

    Mats

    Monday, April 18, 2016 12:32 PM
  • Hi again Eugene,

    I just tried your suggestions, but they didn't help unfortunately.

    Do you know if someone has been successful with this feature in the past?

    Thanks,

    Mats

    Monday, April 18, 2016 1:00 PM
  • Hi Eugene,

    The first link describes interactive creation of formatting rules, which I've got to work as well. The problem I have is when doing it programmatically.

    The second link probably refers to the same problem I have, but it looks that it was not clarified in that thread. Sue's response seems to refer to a different problem.

    Any other hints? Thanks so much for your attempts!

    Mats

    Monday, April 18, 2016 1:11 PM
  • Hello Mats,

    What code exactly do you use now?


    [custom.development]

    Monday, April 18, 2016 2:13 PM
  • Try to create a custom view and then apply auto formatting rules. Does it work?

    [custom.development]

    Monday, April 18, 2016 3:21 PM
  • Have you tried creating a new view and then setting up the auto format properties?

    [custom.development]

    Monday, April 18, 2016 9:58 PM
  • Hi Mats,

    >> I'm having problems with programmatic creation (e.g. VBA) of conditional formatting rules in folders/views in Outlook utizling this API:

    Do you develop with VBA/C# or javascript? Based on your example code, it seems you are using javascript, could you share us which project do you develop?

    >> the Filter property is not properly set it seems, with the result that the rule is applied to all items in the folder, not just the ones selected by the filter expression.

    I suggest you check View->Current View-> View Settings->Conditional Formatting-> check rules for your view->Search for the word after you run your code, and check whether the value set correctly. If it is empty, the rule will be applied to all items.

    Here is a simple code for VBA:

    Sub FilterView()
    
    Dim v As Outlook.TableView
    
    Dim r As Outlook.AutoFormatRule
    
    Set v =   Application.ActiveExplorer.CurrentFolder.CurrentView
    
    Set r = v.AutoFormatRules.Add("test")
    
    r.Font.Underline = True
    
    r.Filter =   """http://schemas.microsoft.com/mapi/proptag/0x0037001f"""   + " LIKE '%thread%'"
    
    r.Enabled = True
    
    v.AutoFormatRules.Save
    
    v.Save
    
    End Sub
    

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, April 19, 2016 5:43 AM
  • Did you call the Apply method right after the Save one?

    Have you tried creating a new view and add auto filtering settings then?


    [custom.development]

    Tuesday, April 19, 2016 11:39 AM
  • There is a solution to this problem, albeit a bit messy.  It is definitely an Outlook bug that causes programmatically created AutoFormatRules (View Conditional Formatting through the Outlook UI) to be created in a semi-corrupt state which won't persist a change of view or re-load of the application.  You can work round the issue by adding an event handler to the Explorer.ViewSwitch event which deletes and re-creates the AutoFormatRule every time the view is changed.  This will make it appear as if your conditional formatting rule is working fine.

    We've used this technique to implement conditional formatting which highlights emails which are pending tracking to Dynamics CRM by having a conditional formatting rule based on the value of crmLinkState custom attribute on the mail items.
        public partial class BrethertonsAddIn
        {
            Outlook.Explorer _activeExplorer;
    
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                _activeExplorer = this.Application.ActiveExplorer();
                _activeExplorer.ViewSwitch += BrethertonsAddIn_ViewSwitch;
                BrethertonsAddIn_ViewSwitch();
            }
    
            private void BrethertonsAddIn_ViewSwitch()
            {
                Outlook.Explorer olExplorer = Application.ActiveExplorer();
                // Convert the Outlook view into a COM _TableView class which as the AutoFormatRules exposed as properties
                // return if this cast cannot be done
                Outlook._TableView tv = olExplorer.CurrentView as Outlook._TableView;
                if (tv == null) return;
                // Try to find an existing AutoFormatRule for CRM Tracking and delete it
                // Use of the loop is necessary as there is no delete method on the AutoFormatRule object
                // So it's necessary to use the Remove method of a collection object instead
                for (int n = tv.AutoFormatRules.Count; n > 1; n--)
                {
                    if (tv.AutoFormatRules[n].Name == "CRM Tracking Pending")
                    {
                        tv.AutoFormatRules.Remove(n);
                    }
                }
                //Add a new rule and then configure it
                Outlook.AutoFormatRule afr = tv.AutoFormatRules.Add("CRM Tracking Pending");
                afr.Filter = "\"http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/crmLinkState\" = 1";
                afr.Font.Italic = true;
                afr.Font.Bold = true;
                afr.Font.Color = Outlook.OlColor.olColorGreen;
                afr.Enabled = true;
                // Save and apply the changes to the rule
                tv.Save();
                afr.Filter = "\"http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/crmLinkState\" = 1";
                tv.Apply();
                afr.Filter = "\"http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/crmLinkState\" = 1";
            }
       }


    Wednesday, January 29, 2020 4:47 PM