Begin main content

Setting Outlook Categories with VBA

Posting this in a bit of a hurry. For context see the previous post How to do tagging in Outlook.

Open up the vba editor (Tools->Macro->Visual Basic Editor). Add a new module and paste the following code in (edit to suit).

Then to add the sub as a toolbar button customise the toolbar in the normal way and choose the 'Macros' category in the left pane - all your applicable macros will appear in the right pane. You can just drag it to your toolbar.

If the button seems to do nothing your outlook security settings may not be allowing macros. See Office Macro Security Settings.

Public Sub TagArchived()
    Dim objOutlook As Outlook.Application
    Dim objInspector As Outlook.Inspector

    Dim strDateTime As String

    ' Instantiate an Outlook Application object.
    Set objOutlook = CreateObject("Outlook.Application")

    ' The ActiveInspector is the currently open item.
    Set objExplorer = objOutlook.ActiveExplorer

    ' Check and see if anything is open.
If Not objExplorer Is Nothing Then' Get the current item.
Dim arySelection As Object
        Set arySelection = objExplorer.Selection
        
        For x = 1 To arySelection.Count
            strCats = arySelection.Item(x).Categories
            If Not strCats = ""Then
                strCats = strCats & ","End If
            strCats = strCats & "archived"
            arySelection.Item(x).Categories = strCats
            arySelection.Item(x).Save
        Next x
        
    Else' Show error message with only the OK button.
        MsgBox "No explorer is open", vbOKOnly
    End If' Set all objects equal to Nothing to destroy them and
' release the memory and resources they take.
    Set objOutlook = Nothing
    Set objExplorer = Nothing
End Sub

06:10 AM, 27 Oct 2006 by Mark Aufflick Permalink

Removing Tags

I use a customized toolbar to tag emails using this code. Right now if it is already tagged "archive" and you run the archive macro again it will tag it as "archive" twice. What is the code for removing the tag if it is already set? Thanks

by Unregistered Visitor on 08/25/09

same question

I have the same question. This thing is great, but it adds multiple categories to a single item. Any way to delete the old one first?

by Unregistered Visitor on 10/23/09

You are a prince among men

I am a PA for dozens of people and at this time of year I used to spend days (no exaggeration) adding all next years meetings, pay days, financial deadlines, school holidays etc to each calendar in turn. Then I discovered the outlook.hol file now I spend days reclassifying all the events so they show up in the right categories on everyone’s To Do lists! With a little bit of replication and tweaking I’ve done this thankless task in one afternoon. Thanks.

by Unregistered Visitor on 11/06/09

That's about the best rap my blog has gotten!

Glad to help :)

by Mark Aufflick on 11/12/09

To replace current categories or remove them...

If you want to replace the current category instead of removing them then delete these lines;

If Not strCats = "" Then
   strCats = strCats & ","
End If
strCats = strCats & "archived"
and replace with this;

strCats = "archived"
Of course you would replace "archived" with whatever category you please.

by Unregistered Visitor on 07/15/10

Error on recurring entries

Hello, I'm using this macro successfully for a while, but it gives me an error message on recurring meetings. Any suggestions to fix this? Thanks Pepijn

by Unregistered Visitor on 11/15/10

RE: Error on recurring meetings

I don't remember ever having a problem with recurring meetings (and goodness knows I've had a few!) Unfortunately (for you - fortunate for me :) I operate a full Apple workflow these days - not a Windows PC to be seen - so I haven't used any Outlook VBA for some time.

by Mark Aufflick on 11/16/10

Pop up once the category is been defined

Hi,

We have a mailbox where we receive multiple requests from clients and these request are being distributed among our team as a task to be worked on. Now I just wanted to know that is it possible to have a popup on users machine when admin assigns any particular mail to that user. I mean once the Category has been set for any particular mail, assignee should get a pop up on his machine saying that "Mail Item1 has been assigned to you".

Thanks,

by Unregistered Visitor on 06/17/11

smal amendment

changed it to a callable procedure and modified it a bit so that the category is not duplicated

Sub SetCat(Text)
    Dim objOutlook As Outlook.Application
    Dim objInspector As Outlook.Inspector

    Dim strDateTime As String

    ' Instantiate an Outlook Application object.
    Set objOutlook = CreateObject("Outlook.Application")

    ' The ActiveInspector is the currently open item.
    Set objExplorer = objOutlook.ActiveExplorer

    ' Check and see if anything is open.
    If Not objExplorer Is Nothing Then
        ' Get the current item.
        Dim arySelection As Object
        Set arySelection = objExplorer.Selection
        
        For x = 1 To arySelection.count
            strCats = arySelection.Item(x).Categories
            If Not strCats = "" Then
                For i = 1 To Len(strCats)
                    If Mid(strCats, i, Len(Text)) = Text Then
                        NextFor = True
                        Exit For
                    End If
                Next i
                strCats = strCats & ","
            End If
            If NextFor = True Then
                NextFor = False
                GoTo NextX
            End If
            strCats = strCats & Text
            arySelection.Item(x).Categories = strCats
            arySelection.Item(x).Save
NextX:
        Next x
        
    Else
        ' Show error message with only the OK button.
        MsgBox "No explorer is open", vbOKOnly
    End If

    ' Set all objects equal to Nothing to destroy them and
    ' release the memory and resources they take.
    Set objOutlook = Nothing
    Set objExplorer = Nothing

End Sub

by Unregistered Visitor on 06/21/11

Thanks for fixing my problem!

I've been trying to do this for ages, and I have written a macro that works for single emails, but not for multiple emails. I've found out the problem was that I was missing: email.Save That's fixed things, thanks!

by Unregistered Visitor on 07/24/11

Similar but different

Hi, I was wondering if it was possible to do a very similar procedure but a little different. There is a shared inbox where multiple emails are sent to a central email address and cced to a specific person. What would be the code to automatically categorize the email to the cced person? Thank you.

by Unregistered Visitor on 09/20/11

Taking up less space

Hi All, I slightly modified this so as to reduce the size of the code when creating multiple tags. This has to be set as a global variable Dim TagName As String Then you create the tag you want Public Sub TagArchive() TagName = "Archive" Call TagVariable End Sub Then rename the master macro as TagVariable Then you replace strCats = strCats & "archived" with strCats = strCats & TagName Then you can make multiple tags and only take up 4 lines, so you don't have to scroll though heaps of code each time you take a look.

by Unregistered Visitor on 07/26/12

Modification request.

How can this be modified so that it toggles the category on/off, taking into account that multiple categories are likely to be present already. Also, it needs to work in Outlook's 'conversation' view when I have the top-level conversation item selected (not an actual message). I will be calling it from Outlook's quick-access toolbar via a shortcut. TYVM for any support you can provide. ^_^

by Unregistered Visitor on 07/25/13

Add comment