Announcement

Collapse
No announcement yet.

Export List Of All Outlook Folders

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Export List Of All Outlook Folders

    Hi all,

    (Outlook) Folders within folders within folders.

    I'm trying to autogenerate a list (into Excel) of the folderpaths of all the folders within my Outlook.

    What I have so far is this:

    Code:
    Public Sub ListOutlookFolders()
         
        Dim olApp As Outlook.Application
        Dim olNamespace As Outlook.Namespace
        Dim olFolder As Outlook.Folders
        Dim objFolder As Outlook.MAPIFolder
        
        Dim rngOut As Range
         
        Set olApp = New Outlook.Application
        Set olNamespace = olApp.GetNamespace("MAPI")
        Set olFolder = olNamespace.Folders
         
        Set rngOut = Range("A1")
         ' Personal Folders
         ' Inbox
         
        For Each objFolder In olFolder
            On Error Resume Next
            rngOut = objFolder.FolderPath
            Set rngOut = rngOut.Offset(1, 0)
        Next
         
        Set rngOut = Nothing
        Set olNamespace = Nothing
        Set olApp = Nothing
         
    End Sub
    which all works great at the first level.

    My question is how do I now make this code loop through all the folders within folders etc...?

    Many thanks,
    I consider myself lucky. I started out with nothing and, well, I still have most of it.

  • #2
    Re: Export List Of All Outlook Folders

    Anybody able to help with this??

    All thoughts appreciated.
    I consider myself lucky. I started out with nothing and, well, I still have most of it.

    Comment


    • #3
      Re: Export List Of All Outlook Folders

      Ok - sorry I couldn't get any help, but have slogged through it myself.

      For the rest of you who might be asking yourselves the same question, here's the code.

      Please note this only works in Office 2003 as the FolderPath method was only available then. If you'd like to see how it works in an earlier version and you have access to Books24x7.com then follow this link

      Don't know why the link function is not working. Here is the full path:

      http://www.books24x7.com/book/id_436...1288&rowid=589

      For those without access to Books24x7.com the details of the book are below:

      Microsoft Outlook Programming: Jumpstart for Administrators, Power Users, and Developers
      by Sue Mosher (ISBN:9781555582869)
      Chapter 12 - Working with Stores and Folders
      Digital Press 2003
      Section 12.4.5 Walking the folder tree to get any folder

      or take a look at her excellent site

      Code:
      'Variable to keep an overall row number
      Public RowNo As Integer
      
      Private Sub CommandButton1_Click()
      
          'Used to keep track of the number of folders
          Dim lCountOfFound As Long
          
          'The output range
          Dim rngPath As Range
      
          'Starting the row number off
          RowNo = 1
      
          'Call this sub procedure
          WalkFolders
      
      End Sub
      
      Sub WalkFolders()
      
          Dim olApp As Outlook.Application
          Dim olSession As Outlook.Namespace
          Dim olStartFolder As Outlook.MAPIFolder
          
          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.
          'or use  GetDefaultFolder(olFolderInbox).Parent instead of the PickFolder
          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
      
      End Sub
      
      Sub ProcessFolder(CurrentFolder As Outlook.MAPIFolder)
      
      Set rngPath = Cells(RowNo, 1)
      
      Dim i As Long
      Dim olNewFolder As Outlook.MAPIFolder
      ' late bind this object variable, since it could be various item types
      Dim olTempFolder As Outlook.MAPIFolder
      Dim olTempFolderPath As String
      
          ' Loop through the items in the current folder.
          ' Looping through backwards in case items are to be deleted,
          ' as this is the proper way to delete items in a collection.
          For i = CurrentFolder.Folders.Count To 1 Step -1
          
          Set olTempFolder = CurrentFolder.Folders(i)
          
              'Puts File Path into Excel here
              olTempFolderPath = olTempFolder.FolderPath
              Cells(RowNo, 1) = olTempFolderPath
              
              RowNo = RowNo + 1
              
              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
      Cheers,
      Last edited by grint; September 4th, 2007, 00:22.
      I consider myself lucky. I started out with nothing and, well, I still have most of it.

      Comment


      • #4
        Re: Export List Of All Outlook Folders

        I know this is very old but I really needed to say thank you for that code.

        Have a nice day.

        Comment


        • #5
          Re: Export List Of All Outlook Folders

          Originally posted by Mikoli View Post
          I know this is very old but I really needed to say thank you for that code.

          Have a nice day.
          Mikoli - glad I could help...
          I consider myself lucky. I started out with nothing and, well, I still have most of it.

          Comment


          • #6
            Re: Export List Of All Outlook Folders

            Originally posted by grint View Post
            Mikoli - glad I could help...
            Hope your still around as I too need this code to work and in trying to run it I get a compile error.

            Compile Error
            Sub or Function not defined

            Sub ProcessFolder(CurrentFolder As Outlook.MAPIFolder)

            Set rngPath = Cells(RowNo, 1)

            Comment


            • #7
              Re: Export List Of All Outlook Folders

              Found another solution which works:

              http://www.msoutlook.info/question/515

              Comment


              • #8
                Re: Export List Of All Outlook Folders

                grint, nice to see your solution & thanks for sharing it after the work you invested in it!
                FYI, looking for a solution too, I bumped into one using a vbs script. Maybe this is handy to know too:
                http://help.wugnet.com/office/print-...ict953144.html
                Take care!

                Comment


                • #9
                  Re: Export List Of All Outlook Folders

                  Great post! Thank you!

                  Comment

                  Working...
                  X