Last reviewed on April 9, 2015   —  14 Comments

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

' to remove categories as you make a list
' uncomment this line
'objNS.Categories.Remove (objCat.CategoryID)
        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

Then run the RestoreCategories macro. The On Error Resume Next line will allow you to add a category list that includes categories already in your master category list, however, the category color won't be updated if the old categories aren't removed.


Public Sub RestoreCategories()

   AddCategory "category", 17, 0

End Sub

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

Using the GetCategoryNames and RestoreCategories Macros

The following video tutorial shows how to use the two macros above to create a list of your categories and their color then add those categories to Outlook. You can use these macros to share category names and colors with other Outlook users.


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

While I recommend using the RestoreCategories code if you need to work with a lot of categories, if you want to add or remove a single category within a macro, you can do it with one line (for each action and category).

If you need to change the category color, you'd first remove it then add it back. The categories already assigned are not affected, the category is removed only from the master category list.

Sub AddRemoveCategories()
    Session.Categories.Remove ("Category2")
    Session.Categories.Add "Category2", 13, 0
End Sub

Get the Categories from All Mailboxes in Profile

This code sample reads every data file in an Outlook profile and grabs the category names, colors and shortcut key, writing the lists to the Immediate window (Ctrl+G) in the format needed by the restore categories macro.

Private Sub GetCategoryNamesinAllAccounts()
    Dim oStores As Outlook.Stores
    Dim oStore As Outlook.Store
    Dim oCategories As Outlook.Categories
    Dim oCategory As Outlook.Category
    Dim strOutput As String
    
    Set oStores = Application.Session.Stores
 
    For Each oStore In oStores
        Set oCategories = oStore.Categories

        If oCategories.Count > 0 Then
        For Each oCategory In oCategories
             strOutput = strOutput & "AddCategory """ & oCategory .Name & """, " _
           & oCategory .Color & ", " & oCategory .ShortcutKey & vbCrLf
        Next
        End If
     
        strOutput = oStore.DisplayName & vbCrLf _
        & "--------------Categories-----------------" & vbCrLf _
        & strOutput
        Debug.Print strOutput
        strOutput = ""

    Next

 Set oStores = Nothing
 Set oStore = Nothing
 Set oCategories = Nothing
 Set oCategory = Nothing
 
End Sub

 

Category Colors by number

The following is a list of the category colors and their index number.

Color Index Color Index
Red 1 Dark Gray 14
Orange 2 Black 15
Peach 3 Dark Red 16
Yellow 4 Dark Orange 17
Green 5 Dark Peach 18
Teal 6 Dark Yellow 19
Olive 7 Dark Green 20
Blue 8 Dark Teal 21
Purple 9 Dark Olive 22
Maroon 10 Dark Blue 23
Steel 11 Dark Purple 24
Dark Steel 12 Dark Maroon 25
Gray 13

Comments

  1. Robert says

    When I run the restore portion I get the error, compile error: Wrong number of arguments or invalid property assignment. Help please as the deleted portion has already worked properly and now I have no categories.

    • Diane Poremsky says

      Are any of the lines in red?

      Did you paste the list of categories in this code, each category is one per line.
      Public Sub RestoreCategories()
      AddCategory "category", 17, 0
      End Sub

  2. Diane Poremsky says

    BTW, this "Wrong number of arguments or invalid property assignment" likely means this line: AddCategory "category", 17, 0 is the problem. You need 3 arguments - the category name in quotes, the color number, and the shortcut code (0 for no shortcut).

  3. Sheryl says

    Sorry, that's a just a fix for the first bug. Here's the fix for the second bug. (Doesn't anyone check the code before uploading?)

    Change this line:

    Private Sub AddCategory()

    to this:

    Private Sub AddCategory(strCategoryName As String, intColor As Integer, intKey As Integer)

    This runs now without error, but it doesn't work as described. It seems to add categories okay (I think), but it doesn't reset the colors, which is why I wanted it. Oh well.

    • Diane Poremsky says

      The necessary code depends on how you are using it. Private means it's not listed in the VBA macro picker - you need ot run it using other code or from the VBA editor. This is useful with code that is only used occasionally so it doesn't clutter up the macro list.

      The colors are represented by the numbers and what is assigned to the category name should be used in outlook.
      AddCategory "work", 17, 0 'orange
      AddCategory "important", 3, 0 'peach
      AddCategory "personal", 8, 0 'blue

      would each use a different color.

  4. Sheryl says

    I wasn't using the macro picker for this. I was not able to run GetCategoryNames from the Immediate Window until I changed the declaration to public. My understanding is that "private" subroutines only can be called by the object or class.

    My color specifications in the RestoreCategories subroutine were copied and pasted from the output of GetCategoryNames, as described in your article. They don't have the comment with the color name at the end (is there a list of numbers to colors somewhere?), but they are otherwise the same. Here is the first line:

    AddCategory "Holiday", 6, 0

    Perhaps the problem is in how I'm trying to use it. I created a Outlook.com "database" (I think it's an OST file) in Outlook, and copied my local calendar entries into it. I very much want to use the same colors in that calendar as the local calendar. The colors for each calendar are stored separately within that calendar's Registry entries (encrypted). There's nothing in the macro that specifies the calendar. Perhaps it's selecting the default calendar, and I haven't changed my default from the local calendar. I tried selecting the Outlook.com calendar then picking the RestoreCategories macro, but no joy.

    Speaking of the Outlook.com calendar (as long as I have your attention), the synchronization problems are driving me crazy. I was able to get it to synchronize JUST ONCE by adding a category called "temp" to all the items, synching, then removing the "temp" category and synching again. 15 minutes or so later, a miracle - the calendar was on the Web. But since then, NOTHING has synched. The changes I make in the local Outlook.com calendar never get synched to the Web. Any thoughts on that?

    Thanks for your help.

    • Diane Poremsky says

      There is a list of color # at http://msdn.microsoft.com/en-us/library/office/bb208064(v=office.12).aspx - I'm not sure it's 100% accurate though, they had a lot of mistakes in the color names previously. It'll match most colors though.

      Private subs can only be called from the module, not across modules. If you are pasting it in the VBA Editor then hitting run, private should work. You can call it from a macro in different module.

      The code works on the default data file (store) - i don't have a code sample that works with other stores, but I do have one that gets the list of colors from all data stores.

      Sub GetColorsAllDataFiles()
      Set objStores = Application.Session.Stores
      For Each objStore In objStores

      Debug.Print vbCrLf & "----------------------------------------" & vbCrLf & objStore.DisplayName
      Debug.Print "--------------Categories-----------------"

      Set objCategories = objStore.Categories
      For Each objCategory In objCategories
      Debug.Print objCategory.Name & ", " & objCategory.Color & ", " & objCategory.ShortcutKey
      Next
      Next

      End Sub

  5. Julian Wattam says

    I used the first bit of code to interrogate the category list and it works great, except that it only works on my main mailbox. Is there a way to get it to look at another mailbox that's been added? I've got 6 email accounts in my profile. They're not set as 'send on behalf of' accounts if you know what I mean - not sure how else to express it. Thanks in advance!

    • Diane PoremskyDiane Poremsky says

      It is, you need to get the data file id first - I'll see if i can put some code together.

  6. Julian Wattam says

    Thanks! I've since been trying to create a button to run a rule and hit the same problem - can only access rules in default mailbox. Hopefully the tweak for the category issue above will work with that too.

    • Diane PoremskyDiane Poremsky says

      I added a macro to the end of the page that creates lists of each data file's category list.

  7. Julian Wattam says

    I've had some problems with GetCategoryNamesinAllAccounts but I think I've kind of solved it. Not sure if part of the problem is my Outlook setup but I can't get it to run without hitting an automation error (or sometimes refers to a non-existent internet calendar sub?). I added a slightly lazy 'on error resume next' to avoid an error.

    You used objCat but this was not defined - should be oCategory.

    With the above tweaks, your code lists mailbox names but the categories are just those from my personal mailbox (which isn't even first on the list). It also stops before finishing. With my revised method below the next store is a personal folders file.

    All I've done in my revised code is stepped back and replace oStore with oStores.item(i) which should surely produce the same result yet does not!

    In my revised code I've got a line which outputs only the categories on the mailbox I'm interested in, which works, but run without this I notice that on some stores it is still reporting categories from my personal mailbox on some stores - perhaps because they're not compatible? There's a public folder for every mailbox, which is to do with our network. Maybe a further tweak can be added to ignore incompatible stores and remove the need for the error 'trap'.

    Anyway, I'm getting what I need, so many thanks for your help. The only issue I have left is that the categories are not listed alphabetically - I'm guessing they are in order of creation.

    Here's my tweaked version of your code:

    Private Sub GetCategoryNamesinAllAccounts()
    Dim oStores As Outlook.Stores
    Dim oStore As Outlook.Store
    Dim oCategories As Outlook.Categories
    Dim oCategory As Outlook.Category
    Dim strOutput As String

    Set oStores = Application.Session.Stores

    On Error Resume Next

    For i = 1 To oStores.Count
    ' If UCase(oStores.Item(i).DisplayName) = Remail Then 'use to find specific account - set Remail to required email address (upper case)
    Set oCategories = oStores.Item(i).Categories
    If oCategories.Count > 0 Then
    For Each oCategory In oCategories
    strOutput = strOutput & oCategory.Name & ", " _
    & oCategory.Color & ", " & oCategory.ShortcutKey & vbCrLf
    Next
    End If

    strOutput = oStores.Item(i).DisplayName & vbCrLf _
    & "--------------Categories-----------------" & vbCrLf _
    & strOutput
    Debug.Print strOutput
    strOutput = ""

    ' End If 'use with IF statement above
    Next i
    Set oStores = Nothing
    Set oStore = Nothing
    Set oCategories = Nothing
    Set oCategory = Nothing
    End Sub

    • Diane PoremskyDiane Poremsky says

      >> You used objCat but this was not defined - should be oCategory
      Oops. After I tested it, i decided to use the code from the other macros to create a list ready to import using the other code and swapped it out.

      Did you previously have a calendar subscription? It sounds like the macro is seeing a data file that used to exist in the profile or exists in a different profile.

    • Diane PoremskyDiane Poremsky says

      BTW, this line:
      If UCase(oStores.Item(i).DisplayName) = Remail then
      probably doesn't work because you aren't using the display name correctly or aren't setting it in the proper format. I personally would not use ucase - either use the display name as it appears or use lcase and set it on both the display name variable and on stores name.
      This works here (shared mailboxes use the mailbox display name, not the email address).
      If oStores.Item(i).DisplayName = "myaddress@mydomain.com" Then

  8. Julian Wattam says

    I think I did mess about with calendar subs once. Maybe if I have a dig around in Outlook's settings I'll find the leftover bits and won't need the on error...

    I used ucase because I'm not sure if the way I see it is the way others see it, ie whether it's in my Outlook profile or comes from the Exchange server. I tend to capitalise on any comparison just in case. It would be just my luck that IT do something that changes the presentation of the address and break my code!

    Thanks again.

    • Webmaster says

      The shared mailbox names come from the Exchange server (as does your own display name) so everyone should see the same thing.

Leave a Reply

Please post long or more complicated questions at OutlookForums by Slipstick.com.

If the Post Comment button disappears, press your Tab key.