0

I would like to achieve the following: A VBA script that would check a shared inbox, every 5 minutes, for emails which have been manually assigned a specific category.

I have no control over the person who assigns the categories and cannot make them use a script or change the procedure in any way.

The goal is to copy each email to a Folder and display a desktop notification.

1 Answers1

1

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.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Thanks Eugene for your contribution. As I am not very experienced with VBA this is somewhat more than I can chew. I will investigate further and try to find an easier solution or code that is similar to my needs. Thanks – user3703618 Mar 10 '22 at 07:50