1
votes

I'm trying to delete all the color categories. However, the following code doesn't always delete all cases, typically leaving two or three.

Any reason it'd skip some categories?

    Public Sub DeleteAllCategories()
        On Error GoTo MyErrorHandler

        'Assume gOutlookApp (as in Dim gOutlookApp As Outlook.Application) is valid
        Dim theCategories As Outlook.Categories
        Set theCategories = gOutlookApp.Session.Categories

        Dim i As Long
        For i = 1 To theCategories.Count
            theCategories.Remove 1
            DoEvents
        Next

        Exit Sub

    MyErrorHandler:
        MsgBox "DeleteAllCategories" & vbCrLf & vbCrLf & "Err = " & Err.Number & vbCrLf & "Description: " & Err.Description
    End Sub
3
This is a guess. Try replacing the For statement with Do While theCategories.Count > 0.Tony Dallimore
A newbie mistake, thanks for pointing it out. I changed it to just that ("Do While theCategories.Count > 0") and even "For i = 1 To 25: theCategories.Remove 1" (catching the errors, too). But it still skips categories. Looks like a timing error but apparently, not. A possible update is needed? Ideas? Anyone?ForEachLoop
I cannot get your code to work at all with Outlook 2003 because, as I understand it, the NameSpace.Categories feature was added for Outlook 2007. (Note, a Session is a NameSpace.) I assumed you were deleting categories from mail items but apparently you are deleting the categories themselves. Is this what you wanted to do?Tony Dallimore
Yes. I may be approaching it wrong though. I am trying to restore categories and colors from a saved list. I was thinking to delete all the existing entries and then re-add my new ones. What could have been simpler, I thought?ForEachLoop
Did you try looping backwards? Are you stepping through the code to see if it is actually deleting each category?JimmyPena

3 Answers

0
votes

This question is a little old, but for someone else who may be searching for the same answer...

Give the below a try:

Public Sub DeleteAllCategories()
    'Assume gOutlookApp (as in Dim gOutlookApp As Outlook.Application) is valid
    Dim objCategory As Outlook.Category   'Changed from Outlook.Categories
    Dim strCAT As String
    On Error GoTo MyErrorHandler

    For Each objCategory In Session.Categories 'Removed the gOutlookApp
        strCAT = objCategory
        Debug.Print strCAT;
        Session.Categories.Remove (strCAT)
        Debug.Print " - Deleted: " & CBool(Session.Categories.Item(strCAT) Is Nothing)
    Next

    Exit Sub

MyErrorHandler:
    MsgBox "DeleteAllCategories" & vbCrLf & vbCrLf & "Err = " & Err.Number & vbCrLf & "Description: " & Err.Description
End Sub
0
votes

I managed to do this using a rule. Something like if its assigned to any category then remove category, and then I ran it on my inbox. A non-coded solution if that interests you!

0
votes

Reverse loop when deleting

Option Explicit
Public Sub DeleteAllCategories()
    Dim theCategories As Outlook.Categories
    Set theCategories = Application.Session.Categories

    Dim i As Long
    For i = theCategories.Count To 1 Step -1
        DoEvents
        Debug.Print theCategories(i)
'       Remove i
    Next
End Sub