December 8, 2021

Outlook: How to automatically categorize emails and meetings based on recipients/sender email domain (VBA)

I share in this post a very convenient way to automatically categorize your emails and appointments
(use case: highlight customer) based on some rules like the email domain used in the distribution list.
This is based on some VBA code.

Problem description

I want my emails and appointment automatically categorized based on some criteria and mainly if the sender or recipients email belongs to a certain domain.
If Recipient or Sender Email contains @Customer1.com then assign category 4Customer1.

It isn't possible to do so by simply using rules (for emails) or conditional formatting for the calendar because of the issues described here and here. Moreover this native approach is quite cumbersome, you would need to define for each customer a pair of rules for both emails and calendar.

I would prefer a more flexible and generic solution where you simply setup the mapping email domain <-> Category and it categorizes everything (email and calendar, email both in the Inbox and Sent Items) automatically without further setup.

I present below a way of achieving this based on some VBA code.

VBA Implementation

The main code for Setting Categories looks like this:

You can get the code from my outlook-vba GitHub repository. It is implemented in Cat.bas module and the ThisOutlookSession.cls (ItemAdd, ItemSend). 

Configuration: Mapping Domain <-> Category

To configure the mapping between domain and category, edit the item in the dictionary in the Cat_InitDic Sub.

Manual Run

To run manually the AutoCategorization you can call the sub macro Cat_CheckCurrentItems

Automatic Setup

To automatically assign categories for new incoming or sent email or added event, you have to use following triggers in ThisOutlookSession:

Set Category for Recurring Appointment: VBA Bug?

For a recurring appointment selected in your Calendar view, on line oItem.Categories = sCat you would get such an error:

The object does not support this method.
This is reported here. I got finally a hint how to fix it from here.

The point is you can not set a category for a recurring appointment if it isn't the Master recurring AppointmentItem. To get the master appointment related to a recurring appointment (if it isn't one) simply get the Parent property of it. 
This is implemented in the Cat->SetCategory function.

Bonus Tip: Use Search Folder based on Category

To finish my setup, I like to use a Search Folder to automatically group customer related emails i.e. based on the category.

In this Search Folder view I remove the Category from the Column Set.

See also

Automatically change Appointment categories using VBA (slipstick)

vba - Setting Outlook appointment item category, for meetings where the invitation came from an external system: Object does not support this method - Stack Overflow

Appointment-Cannot set Categories because ConversationID is not set | Outlook Forums by Slipstick.com



No comments:

Post a Comment