This last Friday I responded to a question on Quora asking if it is possible to export a list of Outlook folders/subfolders. The author goes on to explain that she misplaced a folder and doesn’t want to have to manually look through all her folders to find it. Here’s how I responded
Outlook does not have a built-in capability to export a list of folders/subfolders. There’s no need to though. Search for something you know is in that the missing folder.
As fellow MVP Ben Schorr points out in a comment to my response, my answer is wrong. I don’t know what I was thinking of when I replied. Searching for an item the author knows is in the folder won’t help. Search result do show the name of the folder each matching item is in, but it doesn’t take you to the folder or show you where that folder is located. Clearly it would be handy to have that capability. Time to write some code.
I actually ended up writing two solutions for this issue. Both will work in Outlook 2007 and later. Here they are. Follow these instructions to add the code to Outlook. If you plan to use both solutions, then either place them in different modules or add the code for solution #2 first. The reason for this is that the Dim statement at the top of solution #2 needs to be at the top of a module.
- Start Outlook
- Press ALT+F11 to open the Visual Basic Editor
- If not already expanded, expand Microsoft Office Outlook Objects
- If not already expanded, expand Modules
- Select an existing module (e.g. Module1) by double-clicking on it or create a new module by right-clicking Modules and selecting Insert > Module.
- Copy the code from the code snippet box and paste it into the right-hand pane of Outlook’s VB Editor window
- Click the diskette icon on the toolbar to save the changes
- Close the VB Editor
This solution is intended to be used after a search. It works by taking you directly to the folder an item is in. To use it
- Search for something.
- Select an item in the search results.
- Run this macro.
- The macro will open a separate window with the folder the item is in open in it.
Sub GoToFolder() Const MACRO_NAME = "Go To Folder" Dim olkMsg As Outlook.MailItem, olkFld As Outlook.Folder, olkExp As Outlook.Explorer Select Case Application.ActiveExplorer.Selection.Count Case 0 MsgBox "No items selected. You must select an item before this macro will work.", vbCritical + vbOKOnly, MACRO_NAME Case 1 Set olkMsg = Application.ActiveExplorer.Selection(1) Set olkExp = Application.Explorers.Add(OpenOutlookFolder(olkMsg.Parent.FolderPath)) olkExp.Activate Case Else MsgBox "Multiple items selected. You must select a single item before this macro will work.", vbCritical + vbOKOnly, MACRO_NAME End Select Set olkMsg = Nothing Set olkFld = Nothing Set olkExp = Nothing End Sub Public Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIFolder Dim arrFolders As Variant, _ varFolder As Variant, _ bolBeyondRoot As Boolean On Error Resume Next If strFolderPath = "" Then Set OpenOutlookFolder = Nothing Else Do While Left(strFolderPath, 1) = "\" strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1) Loop arrFolders = Split(strFolderPath, "\") For Each varFolder In arrFolders Select Case bolBeyondRoot Case False Set OpenOutlookFolder = Outlook.Session.Folders(varFolder) bolBeyondRoot = True Case True Set OpenOutlookFolder = OpenOutlookFolder.Folders(varFolder) End Select If Err.Number <> 0 Then Set OpenOutlookFolder = Nothing Exit For End If Next End If On Error GoTo 0 End Function
This solution finds all the folders with a given name. It looks through all of your folders and displays a list of those whose name matches the search string. To use it
- Run this macro.
- Enter the name of the folder to find. The value you enter must exactly match the name of the folder and the search is case sensitive.
- Once the search is complete the macro displays a dialog-box either showing the folders that matched your search or letting you know that it could not find a match. A successful search will show the paths of the matching folders. Paths in Outlook are just like paths in the file system only without a drive letter. The path to any folder is the names of all folders in its hierarchy from the root folder (the mailbox or PST file) of the information store it’s in to the folder itself.
Dim strFolderName As String, strHits As String Sub FindFolder() Const MACRO_NAME = "Find Folder" Dim olkStore As Outlook.Store, olkRoot As Outlook.Folder strHits = "" strFolderName = InputBox("Enter the name of the folder you want to find.", MACRO_NAME) If strFolderName <> "" Then For Each olkStore In Session.Stores Set olkRoot = olkStore.GetRootFolder ProcessFolder olkRoot Next If strHits = "" Then MsgBox "I could not find any folders with the name'" & strFolderName & "'.", vbInformation + vbOKOnly, MACRO_NAME Else MsgBox "I found the following folders with the name '" & strFolderName & "'" & vbCrLf & strHits, vbInformation + vbOKOnly, MACRO_NAME End If End If Set olkRoot = Nothing End Sub Sub ProcessFolder(olkFld As Outlook.Folder) Dim olkSub As Outlook.Folder If olkFld.Name = strFolderName Then strHits = strHits & olkFld.FolderPath & vbCrLf End If For Each olkSub In olkFld.Folders ProcessFolder olkSub Next Set olkSub = Nothing End Sub