none
Loop to Categorize E-Mail based on Read/UNRead Status RRS feed

  • Question

  • I'm attempting to create a VBA Code that will go through all the e-mails in my InBox and based on whether it is Read or Unread, set the Category to READ or UNREAD (Using Rules to do something else with these e-mails).

    Sub CategorizeMail()
     Dim myItem As MailItem
     Dim myFolder As Outlook.MAPIFolder
     Set myNamespace = Application.GetNamespace("MAPI")
     Set myFolder = myNamespace.GetDefaultFolder(olFolderInbox)
    For Each myItem In myFolder
        If myItem.Categories = "UNREAD" Then
            If myItem.UnReadItem = False Then
                myItem.Categories = "READ"
            End If
        End If
    Next
     
    End Sub

    Getting Runtime Error 438 on "For Each myItem In myFolder"... I don't know what I'm doing wrong here.

    Any suggestions / advice would be appreciated.

    Thanks.

    JMData Consultant

    Thursday, February 27, 2014 9:04 PM

Answers

  • Hi JMData,

    What you want is to loop all items in the folder.

    please change the code below:

    For Each myItem In myFolder

    to

    For Each myItem In myFolder.Items


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, February 28, 2014 6:23 AM
    Moderator
  • In case there are items that aren't mail items change your declaration to this:

    Dim myItem As Object

    Then after setting the category save the item. See if that helps.


    Ken Slovak MVP - Outlook

    Friday, February 28, 2014 3:01 PM
    Moderator
  • George / Ken,

    Thank you very much for your assistance.  That appears to have done the trick.

    My code now is:

    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 = "READ"
                myItem.Save
         End If
        
     Next
     
    End Sub

    Appreciate the help end education.

    JMData Consultant

    Friday, February 28, 2014 3:06 PM

All replies

  • Hello JMData,

    The fact is that you declared the mailItem as a MailItem object. But the Inbox folder can contain various types of items.

    Where did you get the UnReadItem property? May be you mean the UnRead property of the MailItem class?

    For Each myItem In myFolder
        If myItem.Categories = "UNREAD" Then
            If myItem.UnRead = False Then
                myItem.Categories = "READ"
            End If
        End If
    Next
    

    Thursday, February 27, 2014 9:12 PM
  • So what is the best way of declaring this?  I tried setting it to object, and that isn't working either.  I'm still learning VBA, so any help would be appreciated.

    Thanks.

    JMData Consultant.

    Thursday, February 27, 2014 9:15 PM
  • Did you try to correct the UnRead property name in the code?

    You may find the Working with Outlook Folders and Items article in MSDN helpful.

    Thursday, February 27, 2014 9:25 PM
  • I'm using Outlook 2010. The article you referenced me to was for 2000.

    I don't know what's wrong with the UnRead Property name of the code.  I'm still learning this.

    Thursday, February 27, 2014 9:27 PM
  • It doesn't matter what version of Outlook you are using at the moment. The VBA syntax was not changed.

    There is no UnReadItem property in the Outlook object model. You need to use the UnRead property of the MailItem class instead.

    Thursday, February 27, 2014 9:38 PM
  • Anyone else have any suggestions???

    Thursday, February 27, 2014 9:41 PM
  • Hi JMData,

    What you want is to loop all items in the folder.

    please change the code below:

    For Each myItem In myFolder

    to

    For Each myItem In myFolder.Items


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, February 28, 2014 6:23 AM
    Moderator
  • Hi JMData,

    Did you try to debug code? Do you get any errors now?

    Friday, February 28, 2014 12:13 PM
  • George,

    Thanks that appears to have done the trick.  However, it appears my code for categorizing the e-mails that have a READ Status isn't working.  I created "READ" and "UNREAD".  I want my code to change the category on the e-mails that have a read status to READ.

    Do you see what is wrong with my code that it's not working? Or do I have to do something different in creating the READ and UNREAD Categories?

    Code as it stands now is below:

    Sub CategorizeMail()
     Dim myItem As MailItem
     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 = "READ"
         End If
        
     Next
     
    End Sub

    Thanks for your help.

    JMData Consultant

    Friday, February 28, 2014 2:48 PM
  • In case there are items that aren't mail items change your declaration to this:

    Dim myItem As Object

    Then after setting the category save the item. See if that helps.


    Ken Slovak MVP - Outlook

    Friday, February 28, 2014 3:01 PM
    Moderator
  • George / Ken,

    Thank you very much for your assistance.  That appears to have done the trick.

    My code now is:

    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 = "READ"
                myItem.Save
         End If
        
     Next
     
    End Sub

    Appreciate the help end education.

    JMData Consultant

    Friday, February 28, 2014 3:06 PM
  • it looks like you have corrected the code now and use the UnRead property as I suggested above:

        If myItem.UnRead = False Then

    >  I want my code to change the category on the e-mails that have a read status to READ.

    That is exactly what you code does. Most probably you don't see categories set in the Outlook window. Am I right?

    If so, you need to customize the fields shown in the explorer window adding the Categories field.

    Anyway, you can find a sample code for setting categories programatically here.

    Friday, February 28, 2014 3:07 PM