How do you delete favorite folders in outlook using VBA

614 views Asked by At

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

There are 1 answers

0
Dick Kusleika On BEST ANSWER

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.