Outlook Object Model: How to identify existing categories and updating/adding new categories in Outlook 2007 using programmatically?

With continuation of my previous–related blog post, I got this task to find out the way how to identify the existing categories and updating/adding new categories in Outlook 2007 using Outlook Object Model programmatically.

I tried the following to find whether the category exists or not:

 Public Function CategoryExists(categoryName As String) As Boolean
  
 Dim category As Outlook.category
  
 Set category = Application.Session.categories(categoryName)
  
 If Not (category Is Nothing) Then
     
 CategoryExists = True
  
 Else
     
 CategoryExists = False
  
 End If
  
 End Function

Then, implement your programming logic, whatever you want to try (update/adding new categories) using OOM. I tried something like this for the category “Cat1” – enclosed the code snippet for your view (code snippet just shows how you can try, not exact implementation):

 Private Sub AddACategory()
  
 Dim categories As Outlook.categories
  
 Set categories = Application.Session.categories
  
 If Not CategoryExists("Cat1") Then
  
 Dim categorynew As Outlook.category
  
 Set categorynew = categories.Add("Cat1", Outlook.OlCategoryColor.olCategoryColorPurple,
 _Outlook.OlCategoryShortcutKey.olCategoryShortcutKeyCtrlF11)
  
 Else
  
 categories.Remove ("Cat1")
 Dim gotcategory As Outlook.category
 Set gotcategorynew = categories.Add("Cat1", Outlook.OlCategoryColor.olCategoryColorDarkGreen, 
_Outlook.OlCategoryShortcutKey.olCategoryShortcutKeyCtrlF11)
    
 End If
 End Sub

Happy programming!!