First of all, you need to run the code every X mins. To get this working you need to use a timer, see Outlook VBA - Run a code every half an hour for more information. To find items with categories you can use the Find
/FindNext
or Restrict
methods of the Items
class. Read more about these methods in the following articles:
Also you may find the Filtering Items article helpful.
For example, the following code uses a DAV Searching and Locating (DASL) query to filter items in the current folder that don't have any category assigned to them. Note that filtering items with an empty string in their categories requires a DASL query; the Microsoft Jet syntax does not support such filters.
Sub NullCategoryRestriction()
Dim oFolder As Outlook.Folder
Dim oItems As Outlook.Items
Dim Filter As String
'DASL Filter can test for null property.
'This will return all items that have no category.
Filter = "@SQL=" & Chr(34) & _
"urn:schemas-microsoft-com:office:office#Keywords" & _
Chr(34) & " is null"
Set oFolder = Application.ActiveExplorer.CurrentFolder
Set oItems = oFolder.Items.Restrict(Filter)
Debug.Print oItems.Count
End Sub
The code sample sets up a DASL filter on the Categories
property, which in the DASL query is expressed in the Office namespace as urn:schemas-microsoft-com:office:office#Keywords
. The filter compares the value of the Categories
property with an empty string using the Is Null
keyword.