VBA: Alternative to Restrict Method with categories?

3k Views Asked by At

My company just moved from outlook 2003 to 2010. We had a button that purple flagged messages and another one that send purple flagged messages to a server database by using flag filter. [FlagStatus] = 2 Since outlook 2010 doesn't use flag color anymore, I changed the code to use category, I successfully create a new category "readyToSend" with purple color. The problem is that I can't filter all the emails with this new category. Microsoft seems to be contradicting themselves and my code doesn't work. I'm looking for an alternative.

from Items.Restrict Method (Outlook)

This method cannot be used and will cause an error with the following properties: Categories

I get that but then if you scroll down to examples you get :

"This Visual Basic for Applications (VBA) example uses the Restrict method to get all Inbox items of Business category and moves them to the Business folder. To run this example, create or make sure a subfolder called 'Business' exists under Inbox. "

Sub MoveItems()  
    Dim myNamespace As Outlook.NameSpace  
    Dim myFolder As Outlook.Folder  
    Dim myItems As Outlook.Items  
    Dim myRestrictItems As Outlook.Items  
    Dim myItem As Outlook.MailItem  

    Set myNamespace = Application.GetNamespace("MAPI")  
    Set myFolder = _  
        myNamespace.GetDefaultFolder(olFolderInbox)  
    Set myItems = myFolder.Items  
    Set myRestrictItems = myItems.Restrict("[Categories] = 'Business'")  
    For i =  myRestrictItems.Count To 1 Step -1  
        myRestrictItems(i).Move myFolder.Folders("Business")  
    Next  
End Sub

I can't get this code to work. solution 1: If i can get this to work, I can solve my problems. solution 2: Find another way to mark messages to transfer/transfered than category

thank you for help, will post code if needed

2

There are 2 best solutions below

1
Max On

if the filter does not work, I would do it other way round:

  • select all items (istead of the already filtered)
  • check each item if it has the category you are looking for

this would look about like this:

Set myItems = myFolder.Items  
''=> this we leave away''Set myRestrictItems = myItems.Restrict("[Categories] = 'Business'")  
For i =  myItems.Count To 1 Step -1  
    if instr(myItems(i).Categories, "readyToSend") <> 0 then myItems(i).Move myFolder.Folders("Business")  
Next 

it might take a Little longer to run, but the diff should not be to bad. I hope this helps, Max

0
user4145213 On

Bit late with an answer, but I have found a working solution at MSDN blog

It needed a bit digging in MFCMapi to get the right string.

For appointment item, the restrict string is: "@SQL=""http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/Keywords"" = '" & sCategoryName & "'"

You need to check if the same string works for message or dig out the correct one.