VBA如何清除Outlook 2010中的收藏夹文件夹列表?

时间:2014-11-24 13:23:30

标签: vba outlook

有没有办法清除VBA中的Outlook收藏列表?

假设我使用以下示例添加文件夹列表:

    Sub Technicians()

    Dim strFolders(0 To 2) As String
strFolders(0) = "\\Derp@derp.com\1. Systems and ApplicationsSystem A"
strFolders(1) = "\\Derp@derp.com\1. Systems and Applications\System B"
strFolders(2) = "\\Derp@derp.com\1. Systems and Applications\System C"

    Dim index As Integer
    Dim folder As Outlook.folder
    Dim objNamespace As NameSpace
    Dim objPane As NavigationPane
    Dim objModule As MailModule
    Dim objGroup As NavigationGroup
    Dim objNavFolder As NavigationFolder


    Set objNamespace = Application.GetNamespace("MAPI")
    Set objPane = Application.ActiveExplorer.NavigationPane
    Set objModule = objPane.Modules.GetNavigationModule(olModuleMail)
        With objModule.NavigationGroups
            Set objGroup = .GetDefaultNavigationGroup(olFavoriteFoldersGroup)
        End With


    index = 0
    For index = 0 To 2
    Set folder = GetFolder(strFolders(index))
        If Not (folder Is Nothing) Then
            Set objNavFolder = objGroup.NavigationFolders.Add(folder)
        End If
    Next index

End Sub

Function GetFolder(ByVal FolderPath As String) As Outlook.folder
    Dim TestFolder As Outlook.folder
    Dim FoldersArray As Variant
    Dim i As Integer

    On Error GoTo GetFolder_Error
    If Left(FolderPath, 2) = "\\" Then
        FolderPath = Right(FolderPath, Len(FolderPath) - 2)
    End If
    'Convert folderpath to array
    FoldersArray = Split(FolderPath, "\")
    Set TestFolder = Application.Session.Folders.Item(FoldersArray(0))
    If Not TestFolder Is Nothing Then
        For i = 1 To UBound(FoldersArray, 1)
            Dim SubFolders As Outlook.Folders
            Set SubFolders = TestFolder.Folders
            Set TestFolder = SubFolders.Item(FoldersArray(i))
            If TestFolder Is Nothing Then
                Set GetFolder = Nothing
            End If
        Next
    End If
    'Return the TestFolder
    Set GetFolder = TestFolder
    Exit Function

GetFolder_Error:
    Set GetFolder = Nothing
    Exit Function
End Function

我可以使用任何可以删除我添加的文件夹的方法或功能吗?

我看到msdn中有一个remove method文件夹,没有给出示例,我不知道如何应用它。

如果无法清除特定列表,我想清除所有收藏夹也可以, 提前谢谢你!

1 个答案:

答案 0 :(得分:0)

Outlook对象模型为Folder类提供Delete方法。以下是MSDN对Delete方法的说明:

Delete方法删除单个文件夹。通常,删除文件夹不需要先删除文件夹中的项目。删除文件夹也会删除文件夹中的所有项目。如果文件夹是无法删除的Outlook文件夹(例如“收件箱”和“已删除邮件”文件夹),则会出现例外情况。在这种情况下,您只能删除文件夹的项目而不能删除文件夹本身。要删除文件夹的Items集合中的所有项目,必须删除从文件夹中的最后一项开始的每个项目。例如,在文件夹的项目集合AllItems中,如果文件夹中有n个项目,则开始删除AllItems.Item(n)中的项目,每次递减索引,直到删除AllItems.Item(1)