Results 1 to 13 of 13

Thread: Categorizing emails based on the subject

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #5
    You could run the AutoCategorize macro below as a script from a rule that applies to all incoming messages, but as you said you were reluctant to use a rule, you could add the following to the ThisOutlookSession module

    Option Explicit
    
    Private WithEvents Items As Outlook.Items
    
    Private Sub Application_Startup()
        Dim olApp As Outlook.Application
        Dim objNS As Outlook.NameSpace
        Set olApp = Outlook.Application
        Set objNS = olApp.GetNamespace("MAPI")
        Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
        lbl_Exit:
        Exit Sub
    End Sub
    
    Private Sub Items_ItemAdd(ByVal item As Object)
        On Error GoTo ErrorHandler
        If TypeName(item) = "MailItem" Then
            AutoCategorize item
        End If
        lbl_Exit:
        Exit Sub
        ErrorHandler:
        MsgBox Err.Number & " - " & Err.Description
        Err.Clear
        GoTo lbl_Exit
    End Sub
    In a normal module enter the modified version of the code below, then restart Outlook (or manually run Application_Startup) to activate the event.
    Public Sub AutoCategorize(olItem As MailItem)
        With olItem
            If InStr(1, olItem.Subject, "[CAT1]", vbTextCompare) > 0 Then
                olItem.Categories = "CAT1"
                olItem.Save
            ElseIf InStr(1, olItem.Subject, "[CAT2]", vbTextCompare) > 0 Then
                olItem.Categories = "CAT2"
                olItem.Save
            ElseIf InStr(1, olItem.Subject, "[CAT3]", vbTextCompare) > 0 Then
                olItem.Categories = "CAT3"
                olItem.Save
            End If
        End With
        lbl_Exit:
        Exit Sub
    End Sub
    'Beers' can be delivered to my web site
    Last edited by Aussiebear; 01-18-2025 at 02:41 PM.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •