Finding an Outlook Folder


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.

  1. Start Outlook
  2. Press ALT+F11 to open the Visual Basic Editor
  3. If not already expanded, expand Microsoft Office Outlook Objects
  4. If not already expanded, expand Modules
  5. 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.
  6. Copy the code from the code snippet box and paste it into the right-hand pane of Outlook’s VB Editor window
  7. Click the diskette icon on the toolbar to save the changes
  8. Close the VB Editor

Solution 1.

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

  1. Search for something.
  2. Select an item in the search results.
  3. Run this macro.
  4. 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

Solution 2.

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

  1. Run this macro.
  2. 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.
  3. 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
Advertisements

17 comments on “Finding an Outlook Folder

  1. More details/correction … , when running from Developer ribbon (OL2016) it says nothing, when choose Macros/Step Into, I get “Sub or Function not defined”.
    Other macros work perfectly.
    Thanks!

    • Hi, juanand.

      My best guess is that something happened during the copy and paste. For example, sometimes that copy and paste loses formatting causing two lines of code to end up on the same line. Another common problem is that single and double quotes along with greater-than, less-than, and ampersands get copied using web formatting. Try this. Delete the code from Outlook. Copy the code again and this time paste it into Notepad. Then copy from Notepad to Outlook. That sometimes fixes some of the issues I mentioned.

  2. Dear David,

    just as Shreyas, I also realize this is an old post, but it is still very interesting. I am inpressed that the code is so concise. I use your code from solution 2 and with the wildcard. I tried to add the found folders to a listbox, but for some reason it adds it 5 times instead of just once. In my case there were two folders it found.
    Also when it finds 6 folders, it puts the folders after one another and at least 5 times.

    I just placed this code here:
    ProcessFolder olkRoot
    UserFormFolderNames.Listbox1.AddItem strHits ‘just this line is new
    Next

    After that I tried adjusting the below line, but I cannot get it to work.
    strHits = strHits & olkFld.FolderPath & vbCrLf
    It should only add unique values to the listbox
    So just one line per found folder.
    Is this possible, how would the code look like?

    Kind regards,
    Richard

    • Hi, Richard.

      UserFormFolderNames.Listbox1.AddItem strHits
      

      You shouldn’t add the results in this way. Doing it this way treats the values in strHits as a single value. You want each value in strHits to be added to listbox individually. Something like this

      arrHits = Split(strHits, vbCrLf)
      For Each varHit in arrHits
          If varHit <> "" Then
              UserFormFolderNames.Listbox1.AddItem varHit
          End If
      Next
      

      Please try that and let me know if it solves the problem.

    • Hi David,

      Thank you very much, this works perfectly. I would not have found this out myself.
      That is the difference between a professional and a wannabe like me.
      The full code for anyone who is wondering about this.
      Private 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
      arrHits = Split(strHits, vbCrLf)
      Next
      For Each varHit In arrHits
      If varHit “” Then
      UserFormFolderNames.ListBox1.AddItem varHit
      End If
      Next
      End If
      If strHits = “” Then
      MsgBox “I could not find any folders with the name'” & strFolderName & “‘.”, vbInformation + vbOKOnly, MACRO_NAME
      Else
      ShowUserFormFolderNames
      End If
      Set olkRoot = Nothing
      End Sub

      I use the UserForm for two things. You can either choose to go to the selected folder or to move the current item to the selected folder.

      The code works perfectly if you just use one mailbox. This question also has been answered on this forum. There is just one downside to this code if you use more than one mailbox and that is the speed. I use 5 mailboxes and then it takes about 20 to 30 seconds before one or more folders are found. Off course this depends on the processor in your computer, but mine isn’t slow.

      David, do you think there is a method which would work faster than the code you have provided? I am not sure if the following suggestion is a possibility, but would it be possible to first add all the folders to a dictionary and then check if the folder to be found matches with one or more items in the dictionary? Or does the current code already do this?

      Kind regards,
      Richard

    • Richard,

      I don’t think loading the folder names into a dictionary and then searching the dictionary would be any faster. The slowest part of the process is looping through all the folders. That has to take place without regard to whether the code checks the name immediately or loads it into a dictionary. The question then seems to come down to whether it’s faster to compare the name immediately or load it into the dictionary. My guess is that there’s no significant difference. Loading the names into a dictionary also presents a problem. Each key value can only occur once in a dictionary. What happens when the same folder name occurs more than once? We’d have to add code to see if the key already exits. If it doesn’t, then we’d add the new key. If it does, then we’d have to append the folder path to the value associated with the key. That extra logic slows the process down by some small amount and, more importantly, complicates the code.

  3. Dear David,

    just as Shreyas, I also realize this is an old post, but still very interesting. I am inpressed that the code is so concise. I use your code from solution 2 and with the wildcard. I tried to add the found folders to a listbox, but for some reason it adds the tow folders 5 times instead of just once. In my case there were two folders it found. But it places the two folders after one another. When it finds 6 instances, there still are 5 lines and an empty one at the beginning and the 6 instances are put after one another.
    I just placed this code here:
    ProcessFolder olkRoot
    UserFormFolderNames.Listbox1.AddItem strHits ‘just this line is new
    Next

    After that I tried adjusting the below line with a lot of different code, but I cannot get it to work.
    strHits = strHits & olkFld.FolderPath & vbCrLf
    It should only add unique values to the listbox
    So just one line per found folder.
    Is this possible?

    Kind regards,
    Richard

    • Hi, Shreyas.

      Yes, it’s easy to make the search ignore case. To do that replace the ProcessFolder subroutine with the version below.

      Sub ProcessFolder(olkFld As Outlook.Folder)
          Dim olkSub As Outlook.Folder
          If LCase(olkFld.Name) = LCase(strFolderName) Then
              strHits = strHits & olkFld.FolderPath & vbCrLf
          End If
          For Each olkSub In olkFld.Folders
              ProcessFolder olkSub
          Next
          Set olkSub = Nothing
      End Sub
      

      We can add a wildcard capability by looking for a substring within the folder name. Something like this. This version looks for whatever you enter within the folder name. For example, if you entered “Project” as the search term, then the search would find all folders that have “project” anywhere within the name.

      Sub ProcessFolder(olkFld As Outlook.Folder)
          Dim olkSub As Outlook.Folder
          If InStr(1, LCase(olkFld.Name), LCase(strFolderName)) > 0 Then
              strHits = strHits & olkFld.FolderPath & vbCrLf
          End If
          For Each olkSub In olkFld.Folders
              ProcessFolder olkSub
          Next
          Set olkSub = Nothing
      End Sub
      
    • Hi, Emil.

      I assume you’re talking about the code in solution 2. If that’s correct, then the way to limit the search to a single mailbox (store in Outlook parlance) is to change this portion of the code from

              For Each olkStore In Session.Stores
                  Set olkRoot = olkStore.GetRootFolder
                  ProcessFolder olkRoot
              Next
      

      to

              Set olkStore = Session.Stores.Item("Store_Name")
              Set olkRoot = olkStore.GetRootFolder
              ProcessFolder olkRoot
      

      where “Store_Name” is the name of the mailbox you want the search to be limited to.

  4. Hi, It’s interesting that in my corporate environment the Solution 2 macro starts with the very large number of public folders, and seems (as a result) never to get round to looking in my own folders. Not sure why at present. Could you offer an explanation for that as it is taking too long to find the folder name entered.

    • Hi, S.

      The search goes through the folders in the order it finds them. I can force it to search in a certain order or can force it to ignore certain folder trees (e.g. Public folders). Would either of those solutions work for you?

  5. Pingback: Quora

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s