我正在尝试删除所有颜色类别。但是,以下代码并不总是删除所有情况,通常会留下两个或三个。
有什么理由会跳过某些类别吗?
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
答案 0 :(得分:0)
这个问题有点老了,但对于那些可能正在寻找相同答案的人来说......
试试以下内容:
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
答案 1 :(得分:0)
我设法使用规则来做到这一点。如果它被分配到任何类别然后删除类别,然后我在我的收件箱中运行它。一个非编码的解决方案,如果你感兴趣的话!
答案 2 :(得分:0)
删除时的反向循环
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