Print a list of your Outlook folders

Last reviewed on October 3, 2012   —  4 comments

Use this VBA macro to get a list of the folder names in your data file, printing it to the Immediate window and inserting it into a new message form.

If you want the full path within the data file, use this line:

strFolders = strFolders & vbCrLf & olTempFolderPath

If you want just the folder names, use

strFolders = strFolders & vbCrLf & olTempFolder

print a folder list in OutlookThe folder list will be in the following format.

While I don't have subfolders in the data file I printed out, the printout will include any subfolders and list the full path.

Macro to print a list of folders in an Outlook data file

to use, open the VB Editor by pressing Alt+F11. Right-click on Project1 and Insert > Module. Paste the following code into the module then run the macro. The folder picker dialog will come up for you to pick the data file (or subfolder) to use as the top level folder for the printout.

Public strFolders As String

Public Sub GetFolderNames()
    Dim olApp As Outlook.Application
    Dim olSession As Outlook.NameSpace
    Dim olStartFolder As Outlook.MAPIFolder
    Dim lCountOfFound As Long

    lCountOfFound = 0
     
    Set olApp = New Outlook.Application
    Set olSession = olApp.GetNamespace("MAPI")
     
     ' Allow the user to pick the folder in which to start the search.
    Set olStartFolder = olSession.PickFolder
     
     ' Check to make sure user didn't cancel PickFolder dialog.
    If Not (olStartFolder Is Nothing) Then
         ' Start the search process.
        ProcessFolder olStartFolder
    End If
    
' Create a new mail message with the folder list inserted
Set ListFolders = Application.CreateItem(olMailItem)
  ListFolders.Body = strFolders
  ListFolders.Display
     
' clear the string so you can run it on another folder
  strFolders = ""
End Sub
 
Sub ProcessFolder(CurrentFolder As Outlook.MAPIFolder)
        
    Dim i As Long
    Dim olNewFolder As Outlook.MAPIFolder
    Dim olTempFolder As Outlook.MAPIFolder
    Dim olTempFolderPath As String
     ' Loop through the items in the current folder.
    For i = CurrentFolder.Folders.Count To 1 Step -1
         
        Set olTempFolder = CurrentFolder.Folders(i)
         
        olTempFolderPath = olTempFolder.FolderPath
         
         'prints the folder path and name in the VB Editor's Immediate window
         Debug.Print olTempFolderPath
          
        ' prints the folder name only
         ' Debug.Print olTempFolder
         
         ' create a string with the folder names.
         ' use olTempFolder if you want foldernames only
         strFolders = strFolders & vbCrLf & olTempFolderPath
        
        lCountOfFound = lCountOfFound + 1
         
    Next
     ' Loop through and search each subfolder of the current folder.
    For Each olNewFolder In CurrentFolder.Folders
         
         'Don't need to process the Deleted Items folder
        If olNewFolder.Name <> "Deleted Items" Then
            ProcessFolder olNewFolder
        End If
         
    Next
     
End Sub

Written by

Diane Poremsky
A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.

Please post long or more complicated questions at Outlookforums.

4 responses to “Print a list of your Outlook folders”

  1. Alex

    Hello Diane,

    Thank you for the code, it was veeeery helpful, You saved my life ;)
    Have a wonderful day!

    Best regards,
    Alex

  2. Nick Alzen

    Hello Diane,

    I want to use the above code to also use the myitem.senton in this case, shouldn't it be oltempfolder.senton? But it says that it doesn't support this method?
    Why?

  3. paul

    That's awesome Diane; today was the day I could really need/use this; thanks for help.

Leave a Reply

If the Post Coment button disappears, press your Tab key.