VBOffice

Categorize Emails Automatically

Let a script categorize every new email.

Last modified: 2007/02/01 | Accessed: 90.930  | #42
◀ Previous sample Next sample ▶
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.

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
    Next

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

  Else
    Item.Categories = AUTO_CATEGORY
    Item.Save
  End If
End Sub
ReplyAll ReplyAll
ReplyAll alerts you before unintentionally replying all, or if you are a confidential BCC recipient of the e-mail.
email  Send a message