D
David C. Holley
How do I loop through my Outlook folders using code? I cannot for the
life of me figure it out.
life of me figure it out.
To able to find a specific folder (regardless of n-level) for use by
couple of SUBS that create/delete AppointmentItems created from Access.
Currently, the SUBs add/delete AppointmentItems in the default Calendar
folder. Since the SUBS are currently dependent upon a specific folder,
the SUBS aren't as flexible as I would like. While they work for me, as
a single-user and developer, I would like to provide the users with the
ability to have the AppointmentItems placed in alternate calendar folders.
Michael said:[top posting corrected]
To able to find a specific folder (regardless of n-level) for use by
couple of SUBS that create/delete AppointmentItems created from Access.
Currently, the SUBs add/delete AppointmentItems in the default Calendar
folder. Since the SUBS are currently dependent upon a specific folder,
the SUBS aren't as flexible as I would like. While they work for me, as
a single-user and developer, I would like to provide the users with the
ability to have the AppointmentItems placed in alternate calendar folders.
It seems to me that Eric's "RecurseFolders()" in his initial response
provides the necessary starting point. Have you tried it?
Michael said:[top posting corrected]
To able to find a specific folder (regardless of n-level) for use by
couple of SUBS that create/delete AppointmentItems created from Access.
Currently, the SUBs add/delete AppointmentItems in the default Calendar
folder. Since the SUBS are currently dependent upon a specific folder,
the SUBS aren't as flexible as I would like. While they work for me, as
a single-user and developer, I would like to provide the users with the
ability to have the AppointmentItems placed in alternate calendar folders.
It seems to me that Eric's "RecurseFolders()" in his initial response
provides the necessary starting point. Have you tried it?
David C. Holley said:I didn't have any subfolders setup when I ran it nor did I have multiple
*.pst files open. I'm going back to my originall opinion that it
shouldn't be this difficult to itierate through ALL folders (include
subfolders). I realized that I was wanting this to be as easy as looping
through the Nodes collection of a TreeView. I shouldn't have to use
multiple SUBs to get the list. If its a FOLDER it should be in the
FOLDERS collection without having to go another layer deeper.
Here's the code that I wrote somewhat inspired by the original...
Sub listOutlookFolders()
'Need to figure out a way to loop subfolders if FOLDERS.COUNT <> 0
'Will probably need to load up an array if multiple folders of the same
name exist
'but will probably just search that folder as well
Dim appOutlook As Outlook.Application
Dim nms As Outlook.NameSpace
Dim mapiParentFolder As Outlook.MAPIFolder
Dim targetFolders As Outlook.Folders
Dim i
Dim j
Set appOutlook = CreateObject("Outlook.Application")
Set nms = appOutlook.GetNamespace("MAPI")
Stop
For i = 1 To nms.Folders.Count
Debug.Print
"-------------------------------------------------------------"
Set mapiParentFolder = nms.Folders(i)
Set targetFolders = mapiParentFolder.Folders
Debug.Print mapiParentFolder.Name
For j = 1 To targetFolders.Count
Call listSubFolders(targetFolders(j))
Next j
Debug.Print
"-------------------------------------------------------------"
Next i
Set targetFolders = Nothing
Set mapiParentFolder = Nothing
Set nms = Nothing
Set appOutlook = Nothing
End Sub
Sub listSubFolders(parentFolder As Object)
Dim i As Integer
Debug.Print parentFolder.Name, parentFolder.Folders.Count
If parentFolder.Folders.Count <> 0 Then
For i = 1 To parentFolder.Folders.Count
Call listSubFolders(parentFolder.Folders(i))
Next i
End If
End Sub
Michael said:[top posting corrected]
Eric Legault [MVP - Outlook] wrote:
What exactly is your intent anyway? Loop n levels deep, or all the way
through? Either way, you have to design the code so that it is recursive - a
function calling itself, like my initial example.
To able to find a specific folder (regardless of n-level) for use by
couple of SUBS that create/delete AppointmentItems created from Access.
Currently, the SUBs add/delete AppointmentItems in the default Calendar
folder. Since the SUBS are currently dependent upon a specific folder,
the SUBS aren't as flexible as I would like. While they work for me, as
a single-user and developer, I would like to provide the users with the
ability to have the AppointmentItems placed in alternate calendar folders.
It seems to me that Eric's "RecurseFolders()" in his initial response
provides the necessary starting point. Have you tried it?