2014-11-24 71 views
0

有沒有辦法清除VBA中的Outlook收藏夾列表?VBA如何清除Outlook 2010中的收藏夾文件夾列表?

比方說,我添加使用下面的示例文件夾列表:

Sub Technicians() 

    Dim strFolders(0 To 2) As String 
strFolders(0) = "\\[email protected]\1. Systems and ApplicationsSystem A" 
strFolders(1) = "\\[email protected]\1. Systems and Applications\System B" 
strFolders(2) = "\\[email protected]\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 

末次

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 

有,我可以使用,將刪除該文件夾的任何方法或函數,我添加?

我看到有一個remove method msdn中的文件夾,沒有給出的例子,我不知道如何應用它。

我想清除所有的收藏夾也可以,如果清除一個特定的列表是不可能的, 先謝謝您!

回答

0

Outlook對象模型爲Folder類提供Delete方法。以下是MSDN爲刪除方法指定的內容:

Delete方法刪除單個文件夾。通常,刪除文件夾不需要先刪除文件夾中的項目。刪除文件夾還會刪除文件夾中的所有項目。如果該文件夾是一個無法刪除的Outlook文件夾,例如收件箱和已刪除郵件文件夾,則會出現異常情況。在這種情況下,您只能刪除文件夾的項目,而不能刪除文件夾本身。要刪除文件夾的Items集合中的所有項目,您必須從文件夾中的最後一個項目開始刪除每個項目。例如,在文件夾的項目集合AllItems中,如果文件夾中有n個項目,則開始刪除AllItems.Item(n)處的項目,每次遞減索引,直到刪除AllItems.Item(1) 。