I am trying to create multiple categories in Outlook 2010 using C#. I am able to successfully generate an executable that will create one category but when I add code to create a second category it will still only add the first one and not the second. If The first category exists it will then add the second one but it will not create both from scratch at the same time.
Below is my code. Any help is greatly appreciated.
using System;
using System.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Microsoft.Office.Interop.Outlook;
namespace OutlookCategory
{
class Program
{
static void Main(string[] args)
{
AddACategory();
}
private static void AddACategory()
{
var app = new Application();
Outlook.Categories categories = app.Session.Categories;
if (!CategoryExists("TEST 1", app))
{
categories.Add("TEST 1", Outlook.OlCategoryColor.olCategoryColorDarkBlue);
}
if (!CategoryExists("TEST 2", app))
{
categories.Add("TEST 2", Outlook.OlCategoryColor.olCategoryColorDarkBlue);
}
}
private static bool CategoryExists(string categoryName, Application app)
{
try
{
Outlook.Category category =
app.Session.Categories[categoryName];
if (category != null)
{
return true;
}
else
{
return false;
}
}
catch { return false; }
}
}
}
I ended up accomplishing this by creating a pipe delimited text file with my category names and their associated category color.
I then looped through the text file creating all of the categories in Outlook.