Search code examples
vbaexceloutlookoutlook-filter

Skip already categorized mails


Current Code:

Dim outlookapp
Dim olns As Outlook.Namespace
Dim Fldr As Outlook.MAPIFolder
Dim olMail As Object

'Dim olMail As Outlook.MailItem
Dim myTasks
Dim projIDsearch As String
Dim myrecipient As Outlook.Recipient
Dim daysAgo As Long

Set outlookapp = CreateObject("Outlook.Application")
Set olns = outlookapp.GetNamespace("MAPI")
Set myrecipient = olns.CreateRecipient("Ccbcphelpdesk")
myrecipient.Resolve

'Set Fldr = olNs.GetDefaultFolder(olFolderInbox).Folders("ExemptionReview")
Set Fldr = olns.GetSharedDefaultFolder(myrecipient, olFolderInbox)
' Restrict search to daysAgo
daysAgo = 0
Set myTasks = Fldr.Items.Restrict("[ReceivedTime]>'" & Format(Date - daysAgo, "DDDDD HH:NN") & "'")
projIDsearch = ActiveCell.Cells(1, 4)

For Each olMail In myTasks
    If (InStr(1, olMail.Subject, projIDsearch, vbTextCompare) > 0) Then
        olMail.Categories = "ESP"
        olMail.Save
    End If
Next
end sub

This looks up emails pertaining to a search string in the subject then tags them as ESP. I need to skip emails that are already categorized.

I have tried:

If (InStr(1, olMail.Subject, projIDsearch, vbTextCompare) > 0) Then
    If olmail.categories Is nothing then 'line returns and error 424
        olMail.Categories = "ESP"
        olMail.Save
    End If
End If

How can I skip Emails that are already categorized and only categorize emails with no category?


Solution

  • https://msdn.microsoft.com/en-us/library/office/ff860423.aspx

    Categories is a String-type property, so test with something like:

    If Len(olmail.Categories) = 0 Then