Outlook macro to create search folder with mail categories as criteria

Ambiorix

New Member
Outlook version
Outlook 2016 64 bit
Email Account
Exchange Server
#1
Hi there,
Since 2010, my workflow is based on the principle of Control Your Day.
Main tools are categories and search folders. I have improved the process by adding my own VBAs to add next actions to the context folder.
The last missing piece I can not get working is the ability to create a search folder based on the assigned categories of a mail.
In other words, when you select an email with categories “Customer” & “BMW” and run the macro, it would create a search folder with “customer” & “BMW” as its criteria.
Thanks a lot in advance and let me know if you want to know more about the Control Your Day way of working!
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#2
I have a macro for to/from sender, it should work to change it to categories.
How to Create an Outlook Search Folder using VBA

You won't need to use the propertyaccessors part - replace the sql string with the proper search string.

Code:
("urn:schemas-microsoft-com:office:office#Keywords" = 'cat1' AND "urn:schemas-microsoft-com:office:office#Keywords" = 'cat2')
to split the categories, we'll steal a bit of code from Macro to Add or Remove a Category


This should work to replace the propertyaccessors part - the folder is named for the category string - which is exactly how it shows up on the selected message.


Code:
 arr = Split(oMail.Categories, ",")
    If UBound(arr) >= 0 Then

' Check for Category
    For i = 0 To UBound(arr)
    strCat1 = arr(0)
    strCat2 = arr(1)
    Next
    End If


Dim strDASLFilter As String


strDASLFilter = "(" & """urn:schemas-microsoft-com:office:office#Keywords""" & "= '" & strCat1 & "' AND" & """urn:schemas-microsoft-com:office:office#Keywords""" & "= '" & strCat2 & "')"
Debug.Print strDASLFilter

Dim strScope As String
strScope = "'Inbox', 'Sent Items'"
    
Dim objSearch As Search
Set objSearch = Application.AdvancedSearch(Scope:=strScope, Filter:=strDASLFilter, SearchSubFolders:=True, Tag:="SearchFolder")

'Save the search results to a searchfolder
objSearch.Save (oMail.Categories)
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#3
I updated the search folder article with a macro for categories.

For the second category, you need to use trim, otherwise there is a leading space and it's not found.
strCat2 = Trim(arr(1))
 

Ambiorix

New Member
Outlook version
Outlook 2016 64 bit
Email Account
Exchange Server
#4
Dear Diane,
Many thanks for your prompt support, it works like a charm!
Warm greetings from Bangkok,
Maarten
 

Similar threads

Top