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
The 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.
Updated March 14 2015 to include the item count after the folder name, like this:
\\Account Manager\Inbox\Completed 95.
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.
When you 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. If you want to list all folders in your data file, choose the top of the data file, which is usually your email address.
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 ' To create a text file you can open in Excel, use this strPath = Environ("USERPROFILE") & "\Documents\OutlookFolders.csv" Debug.Print strPath Set fso = CreateObject("Scripting.FileSystemObject") Set Fileout = fso.CreateTextFile(strPath, True, False) Fileout.WriteLine strFolders ' 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 ' Get the count of items in the folder olCount = olTempFolder.Items.Count 'prints the folder path and name in the VB Editor's Immediate window Debug.Print olTempFolderPath & " " & olCount ' 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 & vbTab & olCount 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
How to use the macros on this page
First: You need to have macro security set to the lowest setting, Enable all macros during testing. The macros will not work with the top two options that disable all macros or unsigned macros. You could choose the option Notification for all macros, then accept it each time you restart Outlook, however, because it's somewhat hard to sneak macros into Outlook (unlike in Word and Excel), allowing all macros is safe, especially during the testing phase. You can sign the macro when it is finished and change the macro security to notify.
To check your macro security in Outlook 2010 and newer, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, look at Tools, Macro Security.
After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.
The macros on this page should be placed in a module.
Open the VBA Editor by pressing Alt+F11 on your keyboard.
To put the code in a module:
- Right click on Project1 and choose Insert > Module
- Copy and paste the macro into the new module.
More information as well as screenshots are at How to use the VBA Editor