Jomili
07-23-2010, 08:37 AM
Hi.
I have literally hundreds of folders within folders within folders, and would like to generate a list (into Excel) of the folderpaths of all the folders within my Outlook. I've been looking on the web, and have only found one possible solution, the code below. It was posted at Ozgrid, but I'm not permitted to post links yet, so can't point directly to it. The guy who posted it never had anyone reply to his thread, so I don't know how good it is.
I've never done macros in Outlook, though I have in Excel, and I can't get his code to work (I'm not even sure how to initiate it). I'd appreciate someone either telling me how to get this code working, or pointing me to some alternative code to achieve the same results. Thanks in advance for your help.
Option Explicit
'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 LongDim
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,
I have literally hundreds of folders within folders within folders, and would like to generate a list (into Excel) of the folderpaths of all the folders within my Outlook. I've been looking on the web, and have only found one possible solution, the code below. It was posted at Ozgrid, but I'm not permitted to post links yet, so can't point directly to it. The guy who posted it never had anyone reply to his thread, so I don't know how good it is.
I've never done macros in Outlook, though I have in Excel, and I can't get his code to work (I'm not even sure how to initiate it). I'd appreciate someone either telling me how to get this code working, or pointing me to some alternative code to achieve the same results. Thanks in advance for your help.
Option Explicit
'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 LongDim
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,