Previous Thread:   Get Received date from a email

9/22/2005 12:09:03 PM    Looping through Folders
How do I loop through my Outlook folders using code? I cannot for the  
  
life of me figure it out.



9/22/2005 4:28:51 PM    Re: Looping through Folders
Now is it just me or does is it just plain *STUPID* that MS didn't give  
  
us an easy way to do this?  
  
Eric Legault [MVP - Outlook] wrote:

9/22/2005 6:37:19 PM    Re: Looping through Folders
I'm thinking more in terms of the Access Object Model where seems a bit  
  
easier to grab stuff. I am about 3/4 through solving the problem with my  
  
own code and it is easier than I thought. My thinking though was related  
  
to the fact that seemed to take multiple levels to get to the folders.  
  
Plus I realized today that this is actually the first time I've had to  
  
do this. All of my other experience with Outlook has been relatively simple.  
  
Eric Legault [MVP - Outlook] wrote:

9/22/2005 6:46:29 PM    Re: Looping through Folders
And here is the initial code...  
  
The final will probably take two parameters - one to designate the  
  
namespace (where to look) and the other the specific folder name (what  
  
to look for). In retrospect, I think that my initial problem had to do  
  
with not realizing that there's a difference between a MAPI folder and  
  
the folders underneath it.  
  
Sub listOutlookFolders()  
  
Dim appOutlook As Outlook.Application  
  
Dim nms As Outlook.NameSpace  
  
Dim mapiParentFolder As Outlook.MAPIFolder  
  
Dim targetFolders As Outlook.Folders  
  
Dim i  
  
Set appOutlook = CreateObject("Outlook.Application")  
  
Set nms = appOutlook.GetNamespace("MAPI")  
  
For i = 1 To nms.Folders.Count  
  
Set mapiParentFolder = nms.Folders(i)  
  
Set targetFolders = mapiParentFolder.Folders  
  
For j = 1 To targetFolders.Count  
  
Debug.Print targetFolders(j).Name, targetFolders(j).Parent  
  
Next j  
  
Next i  
  
Set targetFolders = Nothing  
  
Set mapiParentFolder = Nothing  
  
Set nms = Nothing  
  
Set appOutlook = Nothing  
  
End Sub  
  
Eric Legault [MVP - Outlook] wrote:

9/22/2005 7:10:37 PM    Re: Looping through Folders
I'm thinking that I just flat missed something initially because  
  
nms.Folders(1).Folders(1).Name  
  
is now doing what I was trying earlier. At any rate here is the initial  
  
code...  
  
The final will probably take two parameters - one to designate the  
  
namespace (where to look) and the other the specific folder name (what  
  
to look for). The idea is to be able to be able point my Access resident  
  
SUBS to a specific folder thus making them more generic.  
  
Sub listOutlookFolders()  
  
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  
  
Set mapiParentFolder = nms.Folders(i)  
  
Set targetFolders = mapiParentFolder.Folders  
  
Debug.Print mapiParentFolder.Name  
  
For j = 1 To targetFolders.Count  
  
Debug.Print targetFolders(j).Name  
  
Next j  
  
Next i  
  
Set targetFolders = Nothing  
  
Set mapiParentFolder = Nothing  
  
Set nms = Nothing  
  
Set appOutlook = Nothing  
  
End Sub  
  
Eric Legault [MVP - Outlook] wrote:

9/22/2005 11:17:20 PM    Re: Looping through Folders
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.  
  
Eric Legault [MVP - Outlook] wrote:

9/23/2005 1:27:05 AM    Re: Looping through Folders
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 (and a recursive one at that) to get the list.  
  
Michael Bednarek wrote:

9/23/2005 1:43:07 AM    Re: Looping through Folders
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 Bednarek wrote:

9/23/2005 4:59:11 AM    Re: Looping through Folders
On Thu, 22 Sep 2005 23:17:20 -0400, "David C. Holley"  
  
<DavidCHolley@netscape.net> wrote in  
  
microsoft.public.outlook.program_vba:  
  
[top posting corrected]  
  
It seems to me that Eric's "RecurseFolders()" in his initial response  
  
provides the necessary starting point. Have you tried it?  
  
--  
  
Michael Bednarek  http://mbednarek.com/  "POST NO BILLS"

9/23/2005 11:10:58 AM    Re: Looping through Folders
Yeah that's pretty much it. I had already factored in the issue with  
  
multiple folders. The idea was to go through the folders and if the  
  
folder name matched the only supplied (and probably has the correct  
  
DefaultItemType(property name?) and .ItemCount > 0) then check the  
  
folder. Since my last post, I'm now thinking about storing the target  
  
folder EntryId in a table that I use for system/default values. From  
  
there I'd use the .GetFolderByEntryId to grab it, if the method fails  
  
then I'd use the .PickFolder method to have the user find the folder and  
  
update the system/default table accordingly. The DB example doesnt'  
  
really apply unless you have multiple tables storing the exact data  
  
(breaking 1 of the basic rules of having a relational DB). At any rate  
  
it has been a nice learing experience.  
  
Eric Legault [MVP - Outlook] wrote: