A visitor to Slipstick's Outlook Forums needed a macro to toggle a category on and off. If the category is already on the selected message, the macro removes it, otherwise it adds the category.
In order to remove one category when several are applied to a message, you need to split the categories into an array before deleting the category. After the category is deleted from the array, you'll recreate the category string.
This macro sample sets (or removes) four different categories.
Dim StrCat As String Sub MyBlue() StrCat = "Outlook Users" SetCategory End Sub Sub MyBlue2() StrCat = "Macros" SetCategory End Sub Sub myGreen() StrCat = "Business" SetCategory End Sub Sub myOrange() StrCat = "BB" SetCategory End Sub Private Sub SetCategory() Dim Mail As Object Set Mail = Application.ActiveExplorer.Selection.Item(1) Debug.Print Mail.Categories arr = Split(Mail.Categories, ",") If UBound(arr) >= 0 Then ' Check for Category For i = 0 To UBound(arr) If Trim(arr(i)) = StrCat Then ' remove it arr(i) = "" Mail.Categories = Join(arr, ",") ' Category Removed, exit Exit Sub End If Next End If ' Category not found, add it Mail.Categories = StrCat & "," & Mail.Categories End Sub
If you want to add or remove the categories from a selection of messages, replace the SetCategory sub above with the following.
Private Sub SetCategory() Dim objOL As Outlook.Application Dim currentExplorer As Explorer Dim Selection As Selection Dim Mail As Object Set objOL = Outlook.Application Set currentExplorer = objOL.ActiveExplorer Set Selection = currentExplorer.Selection For Each Mail In Selection Debug.Print Mail.Categories arr = Split(Mail.Categories, ",") If UBound(arr) >= 0 Then ' Check for Category For i = 0 To UBound(arr) If Trim(arr(i)) = StrCat Then ' remove it arr(i) = "" Mail.Categories = Join(arr, ",") ' Category Removed, next message Mail.Save GoTo nextmessage End If Next ' Category not found, add it Mail.Categories = StrCat & "," & Mail.Categories Mail.Save Else ' No Categories, add it Mail.Categories = StrCat & "," & Mail.Categories Mail.Save End If nextmessage: Next 'next message End Sub
How to use the macros on this page
First: You need to have macro security set to the lowest setting, Enable all macros during testing. The macros will not work with the top two options that disable all macros or unsigned macros. You could choose the option Notification for all macros, then accept it each time you restart Outlook, however, because it's somewhat hard to sneak macros into Outlook (unlike in Word and Excel), allowing all macros is safe, especially during the testing phase. You can sign the macro when it is finished and change the macro security to notify.
To check your macro security in Outlook 2010 and newer, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, look at Tools, Macro Security.
After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.
The macros on this page should be placed in a module.
Open the VBA Editor by pressing Alt+F11 on your keyboard.
To put the code in a module:
- Right click on Project1 and choose Insert > Module
- Copy and paste the macro into the new module.
More information as well as screenshots are at How to use the VBA Editor
thank you for the solution! please be so kind to help me extend this from individual email messages to selections of more email messages i.e. to be able to switch on/off categories of all selected messages. thank you!