3

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

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
trixrabbit
  • 259
  • 7
  • 22
  • The example you use is moving items in the inbox to a folder called "Business" when the category is set to "Business". The problem with `Restrict` is only if you have multiple categories as `Categories` field behaves like a Text field. That beinh said, what error are you getting with this code? Does the folder "Business" exists in the same mailbox? – dan Sep 23 '14 at 20:17
  • I get error Execution error '-2147352567 (80020009) Invalid Condition and stops at restrict line. Thx max for the reply, but I'm already passing threw the emails after I am just using filter so I don't have to go threw all emails everytime someone wants to mark and send emails to the server. – trixrabbit Sep 25 '14 at 13:06
  • sorry I did not see your replly as it was not below my answer. In wich line exactly is the error coming, "restrict line" is not quite clear. Maybe set a marker in the code... by the way for me the code works (Outlook 2013) – Max Sep 29 '14 at 20:04

2 Answers2

0

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

Max
  • 744
  • 1
  • 7
  • 19
  • Performance-wise, this could be a problem for users with a lot (hundreds or thousands) of mails (for example, after a long-term absence). – dan Sep 25 '14 at 13:08
0

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.