Importing and Exporting Outlook 2007 Categories

AID: 749
  • Status: Published

10000 points

  • ByBlueDevilFan
  • TypeGeneral
  • Posted on2009-05-31 at 18:48:03
Issue.  "How do I copy my categories to another computer?" and "How can I share a set of categories with a group?" are two of the more frequent category related questions I see here on Experts-Exchange.  The answer is that Microsoft, unfortunately, has not provided a built-in means of backing up or restoring the master category list (MCL), or of sharing an MCL with others.  If you change computers or rebuild your computer, then you are forced to recreate your MCL from scratch.  

Background. Prior to 2007 Outlook stored categories in the registry.  A savvy user could export the registry key containing the MCL to a .reg file.  They could then import that file as needed on a new or rebuilt computer, or share the file with another user.  This provided a rudimentary means of creating a somewhat consistent set of categories within a group.  Microsoft killed this ability in Outlook 2007 when they moved the MCL out of the registry and into a hidden folder in the default mail store (the mailbox or .pst file containing the default inbox).  Fortunately for us the Outlook development team introduced new programming objects that enable building a more effective and efficient solution.  

Solution.  Outlook 2007 introduces the Category and Categories objects to Outlook's object model.  The Category object describes a single category.  The Categories object represents a collection of Category objects.  The Categories object is the MCL.  Using these objects we can build a simple VBA solution that solves both the issue of backing up and restoring, and sharing the MCL. The class module is the heart of this solution.  It provides two methods, one for importing categories and another for exporting them.

The Import method is very simple.  It accepts a single, optional, parameter containing the name and path of the file to import.  If a file name isn't passed, then the code imports from a file named Outlook Categories.txt which it expects to find in you're My Documents folder.  After initializing variables, the code opens the input file and processes the entries it contains one line at a time.  Each entry in the file is composed of a simple list of comma-separated values.  The values are Name, Color, and Shortcut Key.  The code checks each category it reads from the file to see if it already exists.  If it doesn't, then the code adds the category to the Outlook's MCL.

The Export method is even simpler.  It too accepts a single, optional, parameter containing the name and path of the file to use.  As with the Import method, the code defaults to Outlook Categories.txt in you're My Documents folder if a file name isn't passed.  The export is very straightforward.  It simply writes all defined categories to the text file.  The categories are written one per line in the same comma-separated format described above.

Requirements.    Outlook 2007.

Instructions.  Follow these instructions to implement this solution.

1

Add the Class Module


a.  From the menu click Tools > Macro > Visual Basic Editor or press ALT+F11 to open Outlook's VB Editor window.
b.  From the menu click Insert > Class Module.
c.  In the Properties pane (lower left-hand corner) click in the Name field and enter CategoryProcessor.
d.  Copy the code in the snippet below and paste it into the right-hand pane of the VB editor.
e.  Click the diskette icon on the toolbar to save your changes.

Option Explicit

'*** Constants'
Const CB_APPNAME = "CategoryProcessor"
Const CB_VERSION = "1.0"
Const ForReading = 1

'*** Class Variables'
Private bolInitialized As Boolean
Private intCount As Integer
Private objFSO As Object
Private objFile As Object
Private olkCategory As Object
Private strDefaultFilename As String
    
Private Sub Class_Initialize()
    Dim arrVersion As Variant
    arrVersion = Split(Outlook.Application.VERSION, ".")
    If arrVersion(0) < 12 Then
        MsgBox "This object only works with Outlook 2007 and higher.", vbCritical + vbOKOnly, CB_APPNAME
    Else
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        strDefaultFilename = Environ("USERPROFILE") & "\My Documents\Outlook Categories.txt"
        bolInitialized = True
    End If
End Sub

Private Sub Class_Terminate()
    Set objFSO = Nothing
End Sub

Public Sub Export(Optional strFilename As String)
    Dim strFileUsed As String
    If bolInitialized Then
        intCount = 0
        strFileUsed = IIf(strFilename = "", strDefaultFilename, strFilename)
        Set objFile = objFSO.CreateTextFile(strFileUsed, True)
        For Each olkCategory In Outlook.Session.Categories
            objFile.WriteLine olkCategory.Name & "," & olkCategory.Color & "," & olkCategory.ShortcutKey
            intCount = intCount + 1
        Next
        objFile.Close
    End If
    MsgBox "Exported " & intCount & " categories to " & vbCrLf & strFileUsed, vbInformation + vbOKOnly, CB_APPNAME & " - Export"
End Sub

Public Sub Import(Optional strFilename As String)
    Dim strBuffer As String, strFileUsed As String, arrValues As Variant, olkCategory As Object, intRead As Integer
    On Error Resume Next
    If bolInitialized Then
        intCount = 0
        intRead = 0
        strFileUsed = IIf(strFilename = "", strDefaultFilename, strFilename)
        If objFSO.FileExists(strFileUsed) Then
            Set objFile = objFSO.OpenTextFile(strFileUsed, ForReading)
            Do Until objFile.AtEndOfStream
                strBuffer = objFile.ReadLine
                arrValues = Split(strBuffer, ",")
                Set olkCategory = Outlook.Session.Categories.Item(arrValues(0))
                If TypeName(olkCategory) = "Nothing" Then
                    Outlook.Session.Categories.Add arrValues(0), arrValues(1), arrValues(2)
                    intCount = intCount + 1
                End If
                Set olkCategory = Nothing
                intRead = intRead + 1
            Loop
            objFile.Close
            MsgBox "Imported " & intCount & " of " & intRead & " categories read from " & vbCrLf & strFileUsed, vbInformation + vbOKOnly, CB_APPNAME & " - Import"
        Else
            MsgBox "The file " & strFileUsed & " does not exist.  Import aborted.", vbCritical + vbOKOnly, CB_APPNAME & " - Import"
        End If
    End If
    On Error GoTo 0
End Sub
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:

Select allOpen in new window



2

Add the Calling Code


a.  From the menu click Tools > Macro > Visual Basic Editor or press ALT+F11 to open Outlook's Visual Basic editor.
b.  If not already expanded, expand Microsoft Office Outlook Objects.
c.  If not already expanded, expand Modules
d.  Select an existing module (e.g. Module1) by double-clicking on it or create a new module by right-clicking Modules and selecting Insert > Module.
e.  Copy the code from the snippet below and paste it into the right-hand pane of Outlook's VB Editor window

Sub CategoriesExport()
    Dim objCatProcessor As New CategoryProcessor
    With objCatProcessor
        .Export InputBox("Enter the name of the file, including the path, you want to export to.", "Get Export Filename")
    End With
    Set objCatProcessor = Nothing
End Sub

Sub CategoriesImport()
    Dim objCatProcessor As New CategoryProcessor
    With objCatProcessor
        .Import InputBox("Enter the name of the file, including the path, you want to import from.", "Get Import Filename")
    End With
    Set objCatProcessor = Nothing
End Sub
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:

Select allOpen in new window



3

Configure Security


a.  Click Tools > Trust Center.
b.  Click Macro Security.
c.  Set Macro Security to Warnings for all macros.
d.  Click OK.
e.  Close Outlook.
f.  Start Outlook.


4

Using This Solution


a.  Backing-up Your MCL.  Run the CategoriesExport macro.
b.  Restoring Your MCL.  Run the CategoriesImport macro.
c.  Sharing Your MCL.  Create the categories.  Run CategoriesExport to export them to a file.  Distribute the file to your group.  Each group member runs CategoriesImport to import the categories on their computer.



Links to Other BlueDevilFan Articles

1. Creating Linked Notes in Outlook 2007
2. Extending Outlook Rules via Scripting
3. Outlook 2007 Corporate Categories System
4. Automatically Printing/Saving Emails/Attachments in Outlook
5. Avoiding Blank Subject Lines in Outlook
6. Never Again Forget to Add that Attachment to your Outlook Email
7. Enhancing Outlook 2007 Meeting Reminders
Asked On
2009-05-31 at 18:48:03ID749
Tags

Outlook 2007

,

Categories

,

Import

,

Export

,

Share

Topic

Outlook Groupware Software

Views
11378

Comments

Author Comment

by: BlueDevilFan on 2009-06-01 at 16:56:59ID: 1280

Hi, guys.  

Thanks for the feedback.  I'm curious about your first point.

"The first section, "issue", Launches into Categories very quickly as if every reader has had the problems you relay from your EE experiences"

I wrote the article specifically for readers who are familiar with categories and have run into one of the two issues I mentioned.  In my first draft I did talk a bit more about categories.  I removed that as it seemed unnecessary, kind of like explaining what putting is or why it's important in article about how to improve that particular skill.  A reader who is completely unfamiliar with categories probably isn't going to be interested in importing/exporting or sharing them.  They aren't the target audience.  My concern is that explaining the basics of categories in an article targeted at the experienced user risks alienate them.  I know that I'm put off by that sort of writing.  What do you think?

I'll re-read the article and look for the grammatical errors.  I did run it through Word's spelling and grammar checker and thought I'd caught them all.

Expert Comment

by: GegH on 2009-08-24 at 07:58:45ID: 2945

I followed the instructions ran the macro, exported from my machine. Went to someone elses, ran the macro and imported. It worked like a charm, easy to follow instructions as well.

When i restarted Outlook on the second machine the new categories had disappeared and i had to re-import them. Is this supposed to happen? If so how can i get them to stay?

Author Comment

by: BlueDevilFan on 2009-08-24 at 14:27:36ID: 2950

Hi, GegH.

No, that's not what is supposed to happen.  Did you open the same Outlook profile?

Expert Comment

by: GegH on 2009-08-25 at 00:52:14ID: 2954

Yes, same profile. I tried it on a couple of other machines too, same thing.
The mailboxes are on exchange server (sbs 2003), 1 profile per person.

Author Comment

by: BlueDevilFan on 2010-01-04 at 02:15:44ID: 7677

Outlook 2007 SP2 introduces a bug that prevents categories created via code from saving.  Download and install this hotfix to correct the problem: http://support.microsoft.com/kb/970944  Once it's installed the categories should save.

Expert Comment

by: VLNunez on 2010-09-10 at 11:06:16ID: 19254

Fantastic article--thanks so much.  Your information took the work right out of the task!  

Author Comment

by: BlueDevilFan on 2010-09-10 at 11:28:37ID: 19256

Thanks!  Glad it was helpful.

Expert Comment

by: Joshallof on 2011-01-07 at 15:00:49ID: 22728

Is there a way to do a macro to export and import Custom views such as automatic Formating?

Author Comment

by: BlueDevilFan on 2011-01-19 at 14:28:37ID: 22986

This is just a comment to close the loop on Joshallof's post.  My answer is in this question: http://www.experts-exchange.com/Software/Office_Productivity/Groupware/Outlook/Q_26728301.html

Expert Comment

by: jasonburr on 2011-03-18 at 06:51:27ID: 24810

The export worked perfectly, but I receive an error when attempting to import (compile error: Syntax error).  The debugger highlights the line with "Sub CategoriesImport ().   Any ideas?  thanks!

Author Comment

by: BlueDevilFan on 2011-03-31 at 15:21:13ID: 25345

Hi, jasonburr.

Sorry to be slow to get back to you on this.  Can you post a screenshot of the editor window you see when the error occurs?

Expert Comment

by: topprops on 2011-05-31 at 10:20:29ID: 27857

I tried your solution and I get a message that says the list is imported, but it doesn't show up in category list.  I only see the ones I manually put in.  I thought that I was not formating the txt file correctly, so I tried various variations of formating the data.  I get the same result.  330 items imported, but non are on the list.

what am I doing wrong.

Jon

Expert Comment

by: VLNunez on 2011-05-31 at 11:09:17ID: 27858

Try this ... odd as it sounds.  Uncheck your default calendar (another calendar must be checked, though), close Outlook, reopen Outlook after a few seconds allowing for it to close fully, then re-enable your default calendar.  Apparently, the categories live in a hidden file in your default calendar.  This always brings mine back.  I hope Microsoft has fixed this issue in Outlook 2010. I'll find out soon.  Hope this helps, Topprops!  

Expert Comment

by: topprops on 2011-05-31 at 11:32:39ID: 27859

No that didn't help, but thanks

jon

Author Comment

by: BlueDevilFan on 2011-05-31 at 12:20:22ID: 27863

Categories do indeed live in a hidden folder, but not under the calendar.  Categories apply equally to contacts, messages, and tasks as well as to appointments.  

topprops - can you share the file you're trying to import?  If so, then I can test it here to ensure that there aren;t any problems with it.

Expert Comment

by: topprops on 2011-05-31 at 19:10:29ID: 27872

sure,
I tried it several different ways.  here are some

Author Comment

by: BlueDevilFan on 2011-06-02 at 02:40:03ID: 27929

The format of these files are incorrect.  The format should be

CategoryName,CategoryColor,CategoryHotkey

where CategoryColor and CategoryHotkey are numbers.  CategoryColor will be an integer between 1 and 25.  CategoryHotkey will generally be 0 indicating no hotkey.  Each category will be on a line by itself.  Something like this:

Holiday,3,0
Business,2,0
Travel,16,0

Expert Comment

by: topprops on 2011-06-02 at 08:44:42ID: 27934

what number is for categorycolor none?

 I guessed that blank would represent none.

thanks

jon

Author Comment

by: BlueDevilFan on 2011-06-02 at 14:10:43ID: 27949

Use a 0 if you don't want a color.

Expert Comment

by: topprops on 2011-06-02 at 20:13:16ID: 27957

I'' try thatn thanks

Expert Comment

by: topprops on 2011-06-02 at 21:35:31ID: 27958

That worked thanks

jon

Expert Comment

by: topprops on 2011-06-02 at 21:46:51ID: 27959

so now outlook 2007 left the category field off of the input screen for new contacts.  very annoying because now you have to scroll through 330 categories to find the right one.  where as before all you had to do was type in the category or two.  I there any way to add the field to the input screen.  

I'm  being expremely hopeful.

jon

Author Comment

by: BlueDevilFan on 2011-06-08 at 18:38:53ID: 28153

Yes, that's possible.  It means creating a custom form and setting it to be the default contact form.  Here's a page that describes what's involved in doing that.

http://www.outlookcode.com/article.aspx?ID=35

Expert Comment

by: topprops on 2011-06-09 at 07:34:26ID: 28190

Thanks I'll take a look at that.  Never new you could create custom forms.

Jon

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top Outlook Experts

  1. apache09

    663,644

    Sage

    2,168 points yesterday

    Profile
    Rank: Genius
  2. alanhardisty

    170,946

    Guru

    0 points yesterday

    Profile
    Rank: Genius
  3. demazter

    131,854

    Master

    0 points yesterday

    Profile
    Rank: Genius
  4. chris_bottomley

    109,375

    Master

    2,800 points yesterday

    Profile
    Rank: Genius
  5. thinkpads_user

    95,624

    Master

    750 points yesterday

    Profile
    Rank: Genius
  6. Rajkumar-MCITP

    89,780

    Master

    0 points yesterday

    Profile
    Rank: Guru
  7. l33tf0b

    83,091

    Master

    0 points yesterday

    Profile
    Rank: Wizard
  8. BlueDevilFan

    73,191

    Master

    50 points yesterday

    Profile
    Rank: Savant
  9. jjmck

    66,336

    Master

    0 points yesterday

    Profile
    Rank: Genius
  10. Neilsr

    61,466

    Master

    0 points yesterday

    Profile
    Rank: Genius
  11. amitkulshrestha

    61,377

    Master

    0 points yesterday

    Profile
    Rank: Genius
  12. jcimarron

    49,232

    0 points yesterday

    Profile
    Rank: Genius
  13. ve3ofa

    46,002

    0 points yesterday

    Profile
    Rank: Genius
  14. dlmille

    45,200

    0 points yesterday

    Profile
    Rank: Genius
  15. akicute555

    44,979

    10 points yesterday

    Profile
    Rank: Wizard
  16. Anuroopsundd

    44,529

    0 points yesterday

    Profile
    Rank: Sage
  17. HendrikWiese

    40,896

    2,000 points yesterday

    Profile
    Rank: Sage
  18. Exchange_Geek

    37,449

    0 points yesterday

    Profile
    Rank: Sage
  19. jordannet

    36,757

    0 points yesterday

    Profile
    Rank: Wizard
  20. acbrown2010

    34,652

    0 points yesterday

    Profile
    Rank: Genius
  21. diverseit

    34,600

    0 points yesterday

    Profile
    Rank: Guru
  22. WORKS2011

    32,775

    0 points yesterday

    Profile
    Rank: Guru
  23. e_aravind

    31,941

    0 points yesterday

    Profile
    Rank: Genius
  24. JBlond

    31,700

    0 points yesterday

    Profile
    Rank: Sage
  25. limjianan

    30,910

    0 points yesterday

    Profile
    Rank: Genius

Hall Of Fame