How do you delete favorite folders in outlook using VBA

左心房为你撑大大i 提交于 2021-01-28 06:29:44

问题


I wish to delete all the folders from the outlook favorites then subsequently replace them, but the delete doesn't seem to work. What's wrong with my code.

Setup Objects works fine

' Get the "Favorite Folders" navigation group
Set favGroup = Application.ActiveExplorer.NavigationPane.Modules.GetNavigationModule(olModuleMail).NavigationGroups.GetDefaultNavigationGroup(olFavoriteFoldersGroup)

This works

Set inboxFldr = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
favGroup.NavigationFolders.Add (inboxFldr)

All this works except the .delete lines (which fail with the error "wrong number of arguments or invalid property assignment) - obviously I only want one delete line, but put both in to show the options I'd tried.

Debug.Print favGroup.NavigationFolders.count
Set oFolder = Application.ActiveExplorer.CurrentFolder
While favGroup.NavigationFolders.count > 0
    favGroup(1).Delete
    favGroup.NavigationFolders(1).Delete
Wend

回答1:


You have to use the Remove method of the NavigationFolders collection. It takes a NavigationFolder as the argument. There is no Delete method.

Sub RemoveAllFavorites()

    Dim favGroup As NavigationGroup
    Dim favFldrs As NavigationFolders

    Set favGroup = Application.ActiveExplorer.NavigationPane.Modules.GetNavigationModule(olModuleMail).NavigationGroups.GetDefaultNavigationGroup(olFavoriteFoldersGroup)
    Set favFldrs = favGroup.NavigationFolders

    Do While favFldrs.Count > 0
        favFldrs.Remove favFldrs.Item(1)
    Loop

End Sub

I guess it's this way because you're not really deleting anything. The Inbox, for instance, doesn't get deleted just because you remove it from Favorites. That must be why they use a Collection.Remove(Collection.Item) method instead of a Collection.Item.Delete method.



来源:https://stackoverflow.com/questions/31002131/how-do-you-delete-favorite-folders-in-outlook-using-vba

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!