VBA: An alternative to restrict a method using categories?

My company just switched from Outlook 2003 to 2010. We had a purple flagged messages button and another that sends purple flagged messages to the server database using a flag filter. [FlagStatus] = 2 Since outlook 2010 no longer uses the flag color, I changed the code to use the category, I am successfully creating a new category "readyToSend" with purple color. The problem is that I cannot filter all emails with this new category. Microsoft seems to contradict itself and my code is not working. I am looking for an alternative.

from Items.Restrict Method (Outlook)

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

I get this, but then if you go to the examples you get:

"This Visual Basic for Applications (VBA) example uses a constraint method to retrieve all Inbox items from the Business category and move them to the Business folder. To run this example, create or ensure that the Inbox is there is a subfolder called "Business". "

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 = _  
    Set myItems = myFolder.Items  
    Set myRestrictItems = myItems.Restrict("[Categories] = 'Business'")  
    For i =  myRestrictItems.Count To 1 Step -1  
        myRestrictItems(i).Move myFolder.Folders("Business")  
End Sub


I cannot 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 posts for transfer / transfer than categories

Thank you for your help, write the code if needed


source to share

2 answers

If the filter doesn't work, I would do it differently:

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

it will look something 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")  


it might take a little longer to run, but diff shouldn't be bad. Hope this helps, Max



Bit late with an answer, but I found a working solution on the MSDN blog

It took a bit of digging into MFCMap to get the right line.

For the target item, the constraint string is: "@SQL =" " http://schemas.microsoft.com/mapi/string/ {00020329-0000-0000-C000-000000000046} / The keywords" "= '" and sCategoryName and " ""

You need to check if the same line for the message works or digs out the correct one.



All Articles