Dan Ergis
Dan Ergis

Reputation: 385

How to count inbox sub-folders including sub-folders under sub-folders and so on in Outlook

Newbie in need of help. The following code will count all the sub-folders under the inbox but the problem is there are always a number of folders under these sub-folders. I'm having difficulty working out how to count the sub-folders under the sub-folders if you get my meaning :)

MAPIFolder oFolder = this.ns.GetSharedDefaultFolder(oRecip, OlDefaultFolders.olFolderInbox);
int result = oFolder.Folders.Count;
foreach (MAPIFolder subFolder in oFolder.Folders)
   {
       result =+ oFolder.Folders.Count;
   }
tbFolderItemCount.Text = result.ToString();

Upvotes: 1

Views: 819

Answers (1)

hawkke
hawkke

Reputation: 4262

I don't know the API you are using, but you'll need to switch over to a recursive method. I think it might look something like this:

public int CountSubfolders(MAPIFolder folder)
{
    int count = folder.Folders.Count;

    foreach (MAPIFolder subfolder in folder.Folders)
    {
        count += CountSubfolders(subfolder);
    }

    return count;
}

And you'd call it with your root folder:

MAPIFolder oFolder = this.ns.GetSharedDefaultFolder(oRecip, OlDefaultFolders.olFolderInbox);

int subfolders = CountSubfolders(oFolder);

Upvotes: 3

Related Questions