none
ADD CATEGORY TO E-MAIL NOT REPLACE RRS feed

  • Question

  • I have the following code that cycles through my e-mails and categories e-mails as READ if the status is read.

    Sub CategorizeMail()
     Dim myItem As Object
     Dim myFolder As Outlook.MAPIFolder
     Set myNameSpace = Application.GetNamespace("MAPI")
     Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)
     For Each myItem In myFolder.items
         If myItem.UnRead = False Then
                myItem.Categories = ""
                myItem.Categories = "READ"
                myItem.Save
         End If
       
     Next

    However, I recently discovered, that when I put another category to an e-mail that is read, it gets replaced when this procedure is run.  How can I ADD the category if the e-mail already has a category assigned to it?

    Any thoughts on this would be appreciated.

    JMDATA Consultant

    Wednesday, October 28, 2015 2:39 PM

Answers

  • Visual Basic doesn't understand the concatenation that way. It is for C#.

    myItem.Categories = myItem.Categories & ",READ"

    Iterating through all items in the folder can take a lot of time. Methods mentioned in my previous reply to you allow to get a subset of items that correspond to your conditions. So, there is no need to iterate over all items. Try to use them and you will see the difference. This is also particularly important if your code attempts to enumerate more than 256 Outlook items in a collection that is stored on a Microsoft Exchange Server. If you do not release these objects in a timely manner, you can reach the limit imposed by Exchange on the maximum number of items opened at any one time.

    Wednesday, October 28, 2015 4:11 PM

All replies

  • Hello JMData,

    Categories is a delimited string of category names that have been assigned to an Outlook item. This property uses the character specified in the value name, sList, under HKEY_CURRENT_USER\Control Panel\International in the Windows registry, as the delimiter for multiple categories. For example, if the symbol is ; you can concatenate categories into a single string:

    myItem.Categories += ";READ"

    In that case all set categories should be preserved.

    Also don't iterate over all items in the folder. Use the Find/FindNext or Restrict methods of the Items class instead. Read more about these methods in the following articles:

    How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)

    How To: Use Restrict method to retrieve Outlook mail items from a folder

     

    Wednesday, October 28, 2015 3:10 PM
  • Eugene --- Thanks for the response.  I looked in the Register and found that the sList file lists "," as the delimiter.

    I modified the code to be:

    myItem.Categories += ",READ"

    and I'm getting a Compile Error: syntax error.

    Any suggestions?

    Also, I wonder why you're saying not to iterate over all items in the folder. The intent is to go through all the items in my inbox, and mark any item that is read with the category READ so that other rules will activate and sort the e-mails into the respective locations.

    Thanks.

    JMDATA Consultant

    Wednesday, October 28, 2015 4:04 PM
  • Visual Basic doesn't understand the concatenation that way. It is for C#.

    myItem.Categories = myItem.Categories & ",READ"

    Iterating through all items in the folder can take a lot of time. Methods mentioned in my previous reply to you allow to get a subset of items that correspond to your conditions. So, there is no need to iterate over all items. Try to use them and you will see the difference. This is also particularly important if your code attempts to enumerate more than 256 Outlook items in a collection that is stored on a Microsoft Exchange Server. If you do not release these objects in a timely manner, you can reach the limit imposed by Exchange on the maximum number of items opened at any one time.

    Wednesday, October 28, 2015 4:11 PM
  • Eugene --- Thank you... that worked exactly the way I wanted it to.

    As for the iterating, I now understand what you're saying, and will consider it in the future if necessary. Right now this code is running on my own computer and it get run multiple times a day so I'll never reach that limit.

    JMDATA Consulant

    Wednesday, October 28, 2015 4:15 PM