OL 2013 create/add valid AutoFormatRule programmatically with calculated date (e.g. 1 month from now) ? RRS feed

  • Question

  • I've tried creating one with a fixed date using the following VBA (and also through shared add-in), and it does seem to work (at least if no previous AutoFormatRule with same name has been created); However, when I subsequently try to read the string filter for any rule set through code, I get a "something went wrong" error; AutoFormatRules set through code seem devoid of any criteria when viewed through the OL UI, whereas there criteria are always present for any AutoFormatRule set directly for any view using the Outlook UI;

    also, the OL UI allows natural language date specifiers For Filters in ConditionalFormatting, such as "1 month from now", which will always be correct whenever Outlook starts; CAN THIS BE DONE PROGRAMMATICALLY ? IS IT POSSIBLE TO CREATE ANY VALID AUTOFORMATRULE PROGRAMMATICALLY ??

    Thanks for any help.

    Dim olApp as Application

    Set olApp = Application Dim vw As TableView Set vw = olApp.ActiveExplorer.CurrentFolder.CurrentView Dim afr As AutoFormatRule Set afr = vw.AutoFormatRules.Add("New Format Rule") With afr .Filter = Quote(MAPIProp_ID_TagBySetAndHex(PSETID_Tasks, "81050040")) & " <= '2017-03-21 17:08:00'" (TaskDueDate) With .Font .Bold = True End With End With vw.Save vw.Apply

    Tuesday, February 21, 2017 5:53 PM

All replies

  • Hello,

    Before making any change to the AutoFormatRules collection or checking in the UI make sure the current view is a table view:

    ' Check if the current view is a table view. 
     If Application.ActiveExplorer.CurrentView.ViewType = olTableView Then 

    Also I'd suggest checking the Standard property of the AutoFormatRule class which returns  a Boolean value that indicates whether the AutoFormatRule object represents a built-in Outlook formatting rule. If the value of this property is set to True, then the Filter and Name properties of the AutoFormatRule object cannot be changed. Is that the case?

    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Tuesday, February 21, 2017 6:02 PM
  • thanks for the reply...the current view is always a tableview (basic tasks list); if i add a new AutoFormat rule (with a non-standard Outlook name), then it is automatically Standard=false...i.e. a custom/user created AutoFormatRule; I'm not messing around with any of the Outlook built FormatRules...I'd just want to programmatically add a new one, ideally with the natural language string "31 days from now" (or similar), so that it correctly shows up in the Outlook ConditionalFormatting UI; currently it doesn't (no criteria), and trying to read the Filter property gives an error. Any other ideas ? 

    Thanks for your help...


    Wednesday, February 22, 2017 5:47 PM