Search code examples
pythonoutlookpywin32

Restricting by Category whilst working with Outlook in Python


I am currently in the middle of a project which requires me to sort some emails into specific categories.

I have most of it sorted, but I am struggling to filter outlook objects via the restrict method when the "Category" on the email is null.

import win32com.client

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
fmnav = outlook.Folders[1]
inbox = fmnav.Folders[5]
emails = inbox.Items
unread = emails.Restrict("[Unread] = True")
uncat = unread.Restrict("[Categories] = 'NULL'")

The above code first restricts the objects so only unread emails are included, but then I can't work out how to obtain only the objects with no current category.

I have tried a number of combinations of NULL, NONE, and empty strings but to no avail. The docs are reasonably okay with working on filters which have values, and short of doing a long boolean operator saying NOT(...) with the 30+ categories i have, I am out of ideas.

Thanks!


Solution

  • You need to use the SQL syntax:

    @SQL="http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/Keywords/0x0000101F" IS NULL