Categorize Emails Automatically

Let a script categorize every new email.

Last modified: 2007/02/01 | Accessed: 89.087  | #42
◀ Previous sample Next sample ▶
OLKeeper OLKeeper
OLKeeper reliably prevents users from closing their Outlook window and thus possibly missing reminders or e-mails.

With a rule you could assign a category to any new item. But that isn't helpful if you don't know beforehand what to do with the item.

With VBA you're more flexible: For instance, you can assign a category to a new item after it has been moved to a specific folder. In this sample we watch a subfolder of the inbox, and the category name is defined on top in the 'AUTO_CATEGORY' constant.

tip  How to add macros to Outlook
Private WithEvents Items As Outlook.Items

' Automatically assign this category
Private Const AUTO_CATEGORY As String = "(test)"

Private Sub Application_Startup()
  Dim Ns As Outlook.NameSpace
  Dim Inbox As Outlook.MAPIFolder
  Dim Subfolder As Outlook.MAPIFolder

  Set Ns = Application.GetNamespace("MAPI")

  ' Inbox
  Set Inbox = Ns.GetDefaultFolder(olFolderInbox)

  ' Subfolder of the inbox
  Set Subfolder = Inbox.Folders("test")

  Set Items = Subfolder.Items
End Sub

Private Sub Items_ItemAdd(ByVal Item As Object)
  Dim Cats() As String
  Dim i&
  Dim Exists As Boolean

  If Len(Item.Categories) Then
    ' Check whether the category is assigned yet
    Cats = Split(Item.Categories, ";")
    For i = 0 To UBound(Cats)
      If LCase$(Cats(i)) = LCase$(AUTO_CATEGORY) Then
        Exists = True
        Exit For
      End If

    If Exists = False Then
      Item.Categories = Item.Categories & ";" & AUTO_CATEGORY
    End If

    Item.Categories = AUTO_CATEGORY
  End If
End Sub
Category-Manager Category-Manager
With Category-Manager you can group your Outlook categories, share them with other users, filter a folder by category, automatically categorize new emails, and more. You can use the Addin even for IMAP.
email  Send a message