More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

share code 17-Apr-2014 21:59

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

Login Password
Remember me
Register | Send my password
Code level: beginner    Code area: Basic Outlook Printer Friendly Version
Title: Add and remove categories
Description: These two procedures add and remove a specific category from any Outlook item not be searching for text in the Categories property but by handing the categories as an array.
Date: 13-Mar-2006  09:21
Code level: beginner
Code area: Basic Outlook
Posted by: Sue Mosher
This message is displayed as VB.NET
 Sub AddCat(itm, catName)
    arr = Split(itm.Categories, ",")
    If UBound(arr) >= 0 Then
        ' item has categories
        For I = 0 To UBound(arr)
            If Trim(arr(I)) = catName Then
                ' category already exists on item
                ' no need to add it
                Exit Sub
            End If
        Next
        itm.Categories = itm.Categories & "," & catName
    Else
        ' item has no categories
        itm.Categories = catName
    End If
End Sub

Sub RemoveCat(itm, catName)
    arr = Split(itm.Categories, ",")
    If UBound(arr) >= 0 Then
        ' item has categories
        For I = 0 To UBound(arr)
            If Trim(arr(I)) = catName Then
                ' category already exists on item
                ' remove it
                arr(I) = ""
                'rebuild category list from array
                itm.Categories = Join(arr, ",")
                Exit Sub
            End If
        Next
    End If
End Sub
All 1comments
Page [ 1  
  13-Mar-2006  09:23   
Notes on the code:

1) itm is any Outlook item, while catName is a string with the category name.

2) The code assumes that the Categories delimiter is a comma. In some locales, it may be a semicolon, and the code would need to be adjusted accordingly.

3) Typical usage on an Outlook form:

Sub AdjustCatsOnThisItem()
    AddCat Item, "new category"
    RemoveCat Item, "old category"
    Item.Save
End Sub
Page [ 1