Export folder names to a file

Folder List buttonWhile Outlook holds several exporting options for messages, it does not have an option to export a list of folder names. A common workaround suggested is to use screenshots instead. This (an image) is often not a very practical output format.

Even though several screenshot applications also have text recognition and can export the contents to a text file, it usually still is cumbersome. While the end-result will improve, it often isn’t very workable when you have long folder names or a deep rooted system as the names might get truncated in the windows you are taking a screenshot of.

A much more direct solution would be to use a script to generate the txt-file by querying Outlook for the folder names itself. This guide provides 2 methods to do that; via a VBScript file that can run outside of Outlook (quickest method) and by using a macro.


VBS script: Quickest method to export the folder names

VBS Script buttonIf you just want to get your export as quickly as possible use the instructions below;

  1. Download this code-file (exportoutlookfolders.zip)
  2. Open or extract the zip-file and double click on ExportOutlookFolders.vbs
  3. Select the mailbox or folder you want to export the folder names of.
  4. Select whether you want to structure the output or not (see the example screenshots below).
  5. Within a few seconds the file outlookfolders.txt will appear on your Desktop containing the exported folder names.

You’re done! When you want to use the script from within Outlook instead; feel free to continue reading ;-).

Note:
When you run ExportOutlookFolders.vbs again, your new results will be appended to the outlookfolders.txt file.


ExportFolderNames VBA macro

Visual Basic buttonThe ExportFolderNames macro allows you to export all folder names starting from the currently selected folder.

When executing the macro, you’ll be prompted whether or not you want to structure the output.

  • No structuring (Default)
    If you choose “No” (default), the output will list all the folder names with their folder path in full.
  • Structuring
    If you choose “Yes”, only the folder names will be exported. A hyphen character “-” will be placed in front of the folder name to indicate when a folder is a sub folder. A folder that is for instance rooted 3 levels deep when compared to the start folder will have 3 hyphen characters in front of it.

The module also contains a macro called ExportFolderNamesSelect. This macro does exactly the same as the ExportFolderNames with the exception that it will prompt you to select a starting folder. This is mainly to accommodate for exporting folder lists of an entire IMAP mailbox store when using Outlook 2010 (as you can’t select the root folder in that version of Outlook).

Output Examples

Unstructured output of Outlook's folder list
Unstructured output.
Structured output of Outlook's folder list
Structured output.

Quick Install

Use the following instructions to configure the macro in Outlook;

  1. Download this code-file (exportoutlookfolders.zip) or copy the code below.
  2. Open the VBA Editor (keyboard shortcut ALT+F11)
  3. Extract the zip-file and import the ExportFolders.bas file via File-> Import…
    If you copied the code paste it into a new module.
  4. Sign your code so you won’t get any security prompts and the macro won’t get disabled.
  5. Add a button for easy access to the macro or press ALT+F8 and select the macro you want to execute.

ExportFolders icon
Add a button of the macro to the QAT for quick access to it.

Macro Code

The following code is contained in the zip-file referenced in the Quick Install. You can use the code below for review or manual installation.

'==================================================================
'Description: Outlook macro which allows you to export all folder
'	      names starting from the currently selected folder.
'             You can export the folder names with their full path
'	      or as a hierarchical outline.
'
'author : Robert Sparnaaij
'version: 1.0
'website: https://www.howto-outlook.com/howto/exportfoldernames.htm
'==================================================================

Private MyFile As String
Private Structured As Boolean
Private Base As Integer

Public Sub ExportFolderNames()
  Dim F As Outlook.MAPIFolder
  Dim Folders As Outlook.Folders

  Set F = Application.ActiveExplorer.CurrentFolder
  Set Folders = F.Folders
  
  Dim Result As Integer
  Result = MsgBox("Do you want to structure the output?", vbYesNo + vbDefaultButton2 + vbApplicationModal, "Output structuring")
  If Result = 6 Then
    Structured = True
  Else
    Structured = False
  End If
  
  MyFile = GetDesktopFolder() & "\outlookfolders.txt"
  Base = Len(F.FolderPath) - Len(Replace(F.FolderPath, "\", "")) + 1
  
  WriteToATextFile (StructuredFolderName(F.FolderPath, F.Name))
  
  LoopFolders Folders
  
  Set F = Nothing
  Set Folders = Nothing
End Sub

Public Sub ExportFolderNamesSelect()
  Dim F As Outlook.MAPIFolder
  Dim Folders As Outlook.Folders
  
  Set F = Application.Session.PickFolder
  
  If Not F Is Nothing Then
    Set Folders = F.Folders
  
    Dim Result As Integer
    Result = MsgBox("Do you want to structure the output?", vbYesNo + vbDefaultButton2 + vbApplicationModal, "Output structuring")
    If Result = 6 Then
      Structured = True
    Else
      Structured = False
    End If
  
    MyFile = GetDesktopFolder() & "\outlookfolders.txt"
    Base = Len(F.FolderPath) - Len(Replace(F.FolderPath, "\", "")) + 1
    
    WriteToATextFile (StructuredFolderName(F.FolderPath, F.Name))
  
    LoopFolders Folders
  
    Set F = Nothing
    Set Folders = Nothing
  End If
End Sub

Private Function GetDesktopFolder()
  Dim objShell
  Set objShell = CreateObject("WScript.Shell")
  GetDesktopFolder = objShell.SpecialFolders("Desktop")
  Set objShell = Nothing
End Function

Private Sub LoopFolders(Folders As Outlook.Folders)
  Dim F As Outlook.MAPIFolder
    
  For Each F In Folders
    WriteToATextFile (StructuredFolderName(F.FolderPath, F.Name))
    LoopFolders F.Folders
  Next
End Sub

Private Sub WriteToATextFile(OLKfoldername As String)
  fnum = FreeFile()
    
  Open MyFile For Append As #fnum
    Print #fnum, OLKfoldername
  Close #fnum
End Sub

Private Function StructuredFolderName(OLKfolderpath As String, OLKfoldername As String) As String
  If Structured = False Then
    StructuredFolderName = Mid(OLKfolderpath, 3)
  Else
    Dim i As Integer
    i = Len(OLKfolderpath) - Len(Replace(OLKfolderpath, "\", ""))
    
    Dim x As Integer
    Dim OLKprefix As String
    For x = Base To i
      OLKprefix = OLKprefix & "-"
    Next x
    
    StructuredFolderName = OLKprefix & OLKfoldername
  End If
End Function