title: Tagging emails with categories without creating duplicate categories
body: <p>I am attempting to write a rule that applies a category to all incoming mail.</p>
  <p>Ultimately I would like to use this to tag C and C++ related emails with a &quot;Programming - C&quot; and &quot;Programming - C++&quot; tags, but I simplified the example.</p>
  <p>I also want to run this rule on mail that already has the specified categories, but I don't want it to double tag an email that is already tagged.</p>
  <p>Here is the subroutine for adding a category without creating a duplicate:</p>
  <pre><code>' Add a category but don't create duplicates
  Sub AddCategory(ByRef Item As MailItem, strCategory As String)
      Dim exists As Boolean
      Dim arrCategories As Variant
      
      ' Initialize variables
      exists = False
      arrCategories = Split(Item.categories, &quot;,&quot;)
      
      ' Loop through all categories
      For i = LBound(arrCategories) To UBound(arrCategories)
      
          ' Check if the specified category already exists
          If StrComp(strCategory, arrCategories(i)) = 0 Then
              exists = True
              Exit For
          End If
      Next i
  
      ' If the category does not exist, add it
      If Not exists Then
          Item.categories = Item.categories &amp; &quot;,&quot; &amp; strCategory
      End If
  End Sub
  </code></pre>
  <p>The subroutine that calls it:</p>
  <pre><code>Sub filter(Item As MailItem)
      Call AddCategory(Item, &quot;Programming - C&quot;)
      Call AddCategory(Item, &quot;Programming - C++&quot;)
      Item.Save
  End Sub
  </code></pre>
  <p>This filter subroutine is called from a rule and applies to all incoming mail.</p>
  <p>If I run this rule on &quot;All items in Inbox&quot; and an email is already tagged with both &quot;Programming -C&quot; and &quot;Programming - C++&quot; it will duplicate the second tag.</p>
  <p>So if &quot;Programming - C&quot; is the first tag it will duplicate &quot;Programming - C++&quot; and vice versa.</p>
  <p>How can I check existing tags to avoid duplicates?</p>