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
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 'if you have a lot of categories, the immediate window won't be sufficient Open "C:\mycategories.txt" For Append As 1 Print #1, strOutput Close #1 ' 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
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
Restore a text file
This version of RestoreCategories code will read the categories from a text file. You'll either need to edit the text file to remove AddCategory from each line and the quotes from around the category name or create a text file without them.
To create the text file without AddCategory or quotes around the name, change the strOutput line in the GetCategoryNames macro to this
strOutput = strOutput & objCat.name & "," _
& objCat.Color & ", " & objCat.ShortcutKey & vbCrLf
Public Sub RestoreCategories() Dim objNS As NameSpace Dim FileNum As Integer Dim current_cat As String Dim splitCategory() As String Set objNS = Application.GetNamespace("MAPI") On Error Resume Next FileNum = FreeFile() Open "C:\mycategories.txt" For Input As #FileNum While Not EOF(FileNum) Line Input #FileNum, current_cat splitCategory = Split(current_cat, ",") ' use text file, no quotes around name objNS.Categories.Add splitCategory(0), splitCategory(1), splitCategory(2) Wend Close FileNum End Sub
VBScript version to add categories
This is a VBScript version of the macro above.
Dim objOutlook, objNS, objFolder Set objOutlook = CreateObject("Outlook.Application") Set objNS = objOutlook.GetNamespace("MAPI") Dim get_cat Dim splitCategory Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("D:\Documents\categories.txt",1) do while not objFileToRead.AtEndOfStream get_cat= objFileToRead.ReadLine() splitCategory = Split(get_cat, ",") 'If already exists, skip On Error Resume Next ' use text file, no quotes around category name objNS.Categories.Add splitCategory(0), splitCategory(1), splitCategory(2) loop objFileToRead.Close Set objFileToRead = Nothing
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 'if you have a lot of categories, the immediate window won't be sufficient Open "C:\mycategories.txt" For Append As 1 Print #1, strOutput Close #1 strOutput = "" Next Set oStores = Nothing Set oStore = Nothing Set oCategories = Nothing Set oCategory = Nothing End Sub
Set Uniform Categories on All Accounts in Profile
By putting the macros above together, you can create a macro that reads all categories in all accounts in the profile, saving them to a text file, then imports a list of categories to all accounts. In addition, if you need some categories unique to an account, you can set an If statement and add categories only to that account.
Because all of the code needed to do this is listed above (in the Get the Categories from All Mailboxes and RestoreCategories macros), I'm only going to link to a text file containing the macro.
Copy and paste the code into a module, then run the GetCategoryNamesinAllAccounts macro which creates a text file containing all of the category names. You can clean up this list in Excel - paste it into Excel then sort and delete duplicate categories.
After the list is cleaned up, paste it in the AddCategoriesToAllStores macro and run it.
Category Colors by number
The following is a list of the category colors and their index number. Outlook 365 has brighter colors and some new color names, which are noted in the table.
Color | Index | Color | Index |
---|---|---|---|
Red | 1 | New: Gray Old: Dark Gray | 14 |
Orange | 2 | New: Dark Gray Old: Black | 15 |
Peach | 3 | Dark Red | 16 |
Yellow | 4 | Dark Orange | 17 |
New: Light Green Old: Green | 5 | New: Brown Old: Dark Peach | 18 |
New: Light Teal Old: Teal | 6 | New: Gold Old: Dark Yellow | 19 |
New: Lime Green Old: Olive | 7 | Dark Green | 20 |
Blue | 8 | New: Teal Old: Dark Teal | 21 |
New: Lavender Old: Purple | 9 | New: Green Old: Dark Olive | 22 |
New: Magenta Old: Maroon | 10 | New: Navy Blue Old: Dark Blue | 23 |
New: Light Gray Old: Steel | 11 | Dark Purple | 24 |
New: Steel Old: Dark Steel | 12 | New: Dark Pink Old: Dark Maroon | 25 |
New: Warm Gray Old: Gray | 13 |
Hello Diane,
I am dipping my toes into scripting for for Outlook 365 and stumbled across your feeds. They have been very helpful in giving me a baseline to start from. I am trying to create a simple process for my team to use for time tracking and wanted to leverage Outlook meetings and assigned Categories. Would you happen to have a sample script that can return what categories are assigned to a selected Outlook Meeting? My vision is to create a form that pops up and allows people to select/change categories for meetings. We want to make applying categories easier with a form as we have separate groups for the categories (e.g. Group 1 is customer, Group 2 is Country, Group 3 is Internal Tracking, etc...) and people don't like scrolling through the category list. Any help or guidance would be greatly appreciated.
Thank you very much for this clear, comprehensive, and sanity-saving information. Saved me from hours of work, after a company update took apart my categorisation.
Thanks! This is exactly what I have been looking for. I have a lot of categories and I work on multiple computers. I've been looking for a way to keep them in sync.
Is it possible to have the restore code read the list from the file it saved instead of having to open the editor and paste it in each time?
Yes it is. I don't have code handy to do it (i have some i think might work, but i haven't tested it). I'll try to check it tomorrow and post it.
Ok - i added a sample that loads the text file. You need to create a text file without AddCategories or quotes or use find and replace to remove them from an existing text file.
Thanks! I'll give it a try sometime in the next few days. This should make it much easier to keep my categories in sync. I have the category file going to cloud storage so whenever I add/change/delete categories it should be as simple as running the GetCategoryNameswhere I made the change and then RestoreCategories on the other machines.
I have two inboxes on my Outlook. Have managed to extract the list of categories from the original inbox. But when trying to install this list on the new inbox I can't make it do that. It always does the restore to the original inbox.
Have the new inbox as master, but still will not do it. Any tricks?
Terje
Hi Diana,
I have an appointment item open and I set custom color categories and show a msgbox (to check whether to send the meeting request or not, based on the timings - on send click).
When I click on No - the meeting request will not be sent and say red color category is shown. Now I have selected other timings which is still not good time for a meeting, so yellow color category shows.
Expected output, Red color category should be removed from the meeting item and only Yellow should show but, the issue is, both Red and yellow color categories are shown. I checked in debug mode the Item category is shown as empty
I tried removing the categories but it is not removing the current session already aligned category:
Dim category
For Each category In Session.Categories
If (category = "") Then
Session.Categories.Remove ("X")
End If
Next
How can we do this? Appreciate the quick help on this.
Diane,
If I wanted to use this on a shared folder, what are some possibilities to have it grab it from there?
Thanks
I'm not sure if you could make it work with this - https://www.slipstick.com/developer/working-vba-nondefault-outlook-folders/#shared.
Hi, I use categories on my Contacts - I just upgraded to Outlook 2016 - and while all my Categories are still there against the various Contacts the master list of categories and their colours etc have vanished... Is there a way to backwards engiuneer the categories from the contact list...
The category colors are stored on the default data file. Did your profile change? Right click on the top level folder (usually is your email address) and choose properties. Them click the upgrade to color catergores button - the should add the categories to the master list, and assign colors, although the colors wont be the same as before.
If you get is "not in master...." you can click on it and highlight it then click "NEW" this should bring up the New Categories Whit the name of the that one missed colored then select the color you like and save/add it, this should make all other with the same category change to that color.
When moving to a new computer it dose not have the same Index categories list as the old computer this is why that it is not in the Master list, i'm not good at code so i when though and did this for each old Category i had
hope this helps you, and you can under stand my instructions
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.
The shared mailbox names come from the Exchange server (as does your own display name) so everyone should see the same thing.