Create a list of color categories and merge or restore the list using VBA

Last reviewed on October 3, 2012

Applies to: Microsoft Outlook 2013, Outlook 2010, Outlook 2007

The code samples on this page will get the category names, color, and shortcuts assigned. Once you have this list, you can use the second code sample to restore the list to a different computer. If you want a simple list of your categories and color names that are assigned, see Print a list of Outlook Categories and their Colors. For a list of utilities you can use to manage your color categories, see the Tools section at Outlook Categories and Color Categories.

Step 1: Get a formatted list of color categories.
Step 2: Add the formatted list to the AddCategory Macro and run the macro

Begin by running the GetCategoryNames macro. Press Ctrl+G to open the Immediate window then select all and copy the list. Paste the list into Notepad or a plain text email message.

If the categories names are not unique, the macro AddCategory macro will fail. You can use the DeleteCategories macro to remove all categories before adding your list of categories

Run the macro to create a formatted list of color categories


Private Sub GetCategoryNames()
    Dim objNS As NameSpace
    Dim objCat As Category
    Dim strOutput As String
    
    Set objNS = Application.GetNamespace("MAPI")
    
    If objNS.Categories.Count > 0 Then
        
        For Each objCat In objNS.Categories
           strOutput = strOutput & "AddCategory """ & objCat.Name & """, " _
           & objCat.Color & ", " & objCat.ShortcutKey & vbCrLf
        Next
    End If
    
    ' Print the list to the Immediate Window
    ' Press Ctrl+G to open it or use View > Immediate Window
   Debug.Print strOutput
    
    ' Clean up.
 Set objCat = Nothing
 Set objNS = Nothing
End Sub

Restore the color categories list

Copy the list created by the code above and paste it into this code sample, in place of
AddCategory "category", 17, 0

Add the formatted list to the AddCategories macro and run it


Public Sub RestoreCategories()

   AddCategory "category", 17, 0

End Sub

Private Sub AddCategory()
    Dim objNS As NameSpace
 
    Set objNS = Application.GetNamespace("MAPI")
  
    objNS.Categories.Add strCategoryName, intColor, intKey
    Set objNS = Nothing
End Sub

Delete color categories

To delete the existing categories and add the categories in your list, paste the following code into the VBA editor and add DeleteCategories as the first line of the RestoreCategories procedure:

Public Sub RestoreCategories()
DeleteCategories
AddCategory "category", 17, 0
End Sub

Warning: using the DeleteCategories procedure will delete all categories from the list.

Private Sub DeleteCategories()
    Dim objNS As NameSpace
    Dim objCat As Category
   
    Set objNS = Application.GetNamespace("MAPI")
   
    If objNS.Categories.Count > 0 Then
       
        For Each objCat In objNS.Categories
            objNS.Categories.Remove (objCat.CategoryID)
        Next
       
    End If
       
    Set objCat = Nothing
    Set objNS = Nothing
   
End Sub

Written by

Diane Poremsky
A Microsoft Outlook Most Valuable Professional (MVP) since 1999 and involved in IT support since 1985, Diane is the author of several books and video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.