outlook-vba Introduction Part 3: Stores and all their folders 3.4 Listing the names of every folder within every accessible store

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

In Part 2, you were shown how to list every accessible store and the top level folders within each store. This involved a loop through the stores and then a loop for each store through its folders Above you have seen how to reference a known folder at any depth within the hierarchy of folders. This involved chaining together as many Folders("x")s as necessary to reach the folder.

I now want to list every folder, at any depth, within every store. The easiest coding technique for solving this type of problem where you must move down chains of varying lengths is recursion. If you are a serious programmer in another language or tool, you may already know about recursion. If you have ambitions to be a serious programmer, you will need to understand recursion eventually but not necessarily today. “Recursion” is one of those concepts that many find difficult to grasp at first. You can type “Recursion” into your favourite search engine and read the various attempts at explaining this concept. Alternatively, you can accept these macro work but not worry how they work.

Note the comment in ListStoresAndAllFolders(): these macros need a reference to “Microsoft Scripting Runtime”. Click Tools in the tab bar at the top of the VB Editor window then click References. You will get a list of all the available references (libraries). Some at the top will already be ticked. The remainder are in alphabetic order. Scroll down the list and click the box to the left of “Microsoft Scripting Runtime” to get a tick. Then click OK

 Sub ListStoresAndAllFolders()

  ' Displays the name of every accessible store
  ' Under each store, displays an indented list of all its folders

  ' Technique for locating desktop from answer by Kyle:
  ' http://stackoverflow.com/a/17551579/973283

  ' Needs reference to “Microsoft Scripting Runtime” if "TextStream"
  ' and "FileSystemObject" are to be recognised

  Dim FileOut As TextStream
  Dim FldrCrnt As Folder
  Dim Fso As FileSystemObject
  Dim InxFldrCrnt As Long
  Dim InxStoreCrnt As Long
  Dim Path As String
  Dim StoreCrnt As Folder

  Path = CreateObject("WScript.Shell").SpecialFolders("Desktop")
  
  Set Fso = CreateObject("Scripting.FileSystemObject")
  Set FileOut = Fso.CreateTextFile(Path & "\ListStoresAndAllFolders.txt", True)

  With Application.Session
    For InxStoreCrnt = 1 To .Folders.Count
      Set StoreCrnt = .Folders(InxStoreCrnt)
      With StoreCrnt
        FileOut.WriteLine .Name
        For InxFldrCrnt = .Folders.Count To 1 Step -1
          Set FldrCrnt = .Folders(InxFldrCrnt)
          Call ListAllFolders(FldrCrnt, 1, FileOut)
        Next
      End With
    Next
  End With

  FileOut.Close

End Sub
Sub ListAllFolders(ByRef Fldr As Folder, ByVal Level As Long, ByRef FileOut As TextStream)

  ' This routine:
  '  1. Output name of Fldr
  '  2. Calls itself for each child of Fldr
  ' It is designed to be called by ListStoresAndAllFolders()

  Dim InxFldrCrnt As Long

  With Fldr
    FileOut.WriteLine Space(Level * 2) & .Name
    For InxFldrCrnt = .Folders.Count To 1 Step -1
      Call ListAllFolders(.Folders(InxFldrCrnt), Level + 1, FileOut)
    Next
  End With

End Sub

After you have run ListStoresAndAllFolders, there will be a new file on your DeskTop named “ListStoresAndAllFolders.txt” which will contain the promised list of stores and folders.



Got any outlook-vba Question?