12

There doesn't seem to much information or any good code samples for setting an Outlook 2007 MailItem's categories programmatically.

MSDN has a limited page, and mentions using VB's Split function, saying more or less "you're on your own from here onwards, so sort it out yourself".

So far as I can tell we manipulate the Categories as a comma delimited string property of the mailitem. It seems a bit primitive, is that all there is to it?

Does everyone just dig out their library of string functions and parse the Categories property, trusting not to get in a muddle when multiple categories are set for a single mailitem and (heaven forbid) categories are renamed?

hawbsl
  • 15,313
  • 25
  • 73
  • 114

2 Answers2

23

You can choose to manipulate the comma-delimited string of Categories any way you choose. To insert a category, I usually check if the current string is null and then just assign it. If the category is not null then I append it if it doesn't already exist. To remove an item, I just replace the category name with an empty string.

Adding Category

 var customCat = "Custom Category";
 if (mailItem.Categories == null) // no current categories assigned
   mailItem.Categories = customCat;
 else if (!mailItem.Categories.Contains(customCat)) // insert as first assigned category
   mailItem.Categories = string.Format("{0}, {1}", customCat, mailItem.Categories);

Removing Category

var customCat = "Custom Category";
if (mailItem.Categories.Contains(customCat))
  mailItem.Categories = mailItem.Categories.Replace(string.Format("{0}, ", customCat), "").Replace(string.Format("{0}", customCat), "");

There are multitudes of ways to manipulate strings - they just chose to keep the serialized data structure simple underneath.

I tend to create my own Categories during Add-in startup to verify they exist. Certainly - category renaming is a concern, but if you are ensuring that your categories exist each time your add-in loads, you can at least ensure some level of validity.

To manage Outlook Categories, you can use ThisAddIn.Application.Session.Categories.

var customCat = "Custom Category";
if (Application.Session.Categories[customCat] == null)  
  Application.Session.Categories.Add(customCat, Outlook.OlCategoryColor.olCategoryColorDarkRed);
SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
  • Hi, I just tried doing this, however the category doesn't update. It runs the instruction `mailItem.Categories = customCat;` with no problems, but it doesn't show in Outlook. Should I save or something? – Soph Oct 01 '12 at 15:40
  • 4
    You need to issue a [`MailItem.Save()`](http://msdn.microsoft.com/en-us/library/office/ff866979.aspx) to persist any metadata changes with [`MailItems`](http://msdn.microsoft.com/en-us/library/office/ff861332.aspx) – SliverNinja - MSFT Oct 01 '12 at 16:16
  • Thanks! Simple yet I had completely missed that! – Soph Oct 03 '12 at 14:21
  • 4
    FYI according to this: http://msdn.microsoft.com/en-us/library/office/ff860423(v=office.15).aspx the delimiter is localized (which is a dumb idea) `This property uses the character specified in the value name, sList, under HKEY_CURRENT_USER\Control Panel\International in the Windows registry, as the delimiter for multiple categories.` – The Muffin Man May 28 '14 at 18:29
  • Note you can ensure you are using the current culture "List dilimiter" with mailItem.Categories = $"{customCat}{System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator} {mailItem.Categories}"; – Jows Sep 01 '17 at 01:46
0

Just to include one last information inside of the @SilverNija - MSFT post. After do something like this:

 var customCat = "Custom Category";
 if (mailItem.Categories == null) // no current categories assigned
   mailItem.Categories = customCat;
 else if (!mailItem.Categories.Contains(customCat)) // insert as first assigned category
   mailItem.Categories = string.Format("{0}, {1}", customCat, mailItem.Categories);

Don't forget and it is the most important, to update the instance of the mailItem this way:

mailItem.Save();

Because, there is an issue that sometimes a bunch of mail items are not updating when they are inside of a loop, so it worked for me to solve this problem.

Junior Grão
  • 1,361
  • 1
  • 8
  • 7