Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6985465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:41:02+00:00 2026-05-27T18:41:02+00:00

Using the Microsoft Outlook 14.0 Object Library reference and the code below I would

  • 0

Using the Microsoft Outlook 14.0 Object Library reference and the code below I would like to count all folders listed under the mailbox including every subfolder but I have a problem.

enter image description here

This code only counts the top level folders and second level folder but doesn’t count any subfolders beneath. Something wrong within my foreach statement in the countRootFolders method but I can’t work it out. Can anyone help?

Microsoft.Office.Interop.Outlook.Application app = null;
Microsoft.Office.Interop.Outlook._NameSpace ns = null;

private void button1_Click(object sender, EventArgs e)
{
    app = new Microsoft.Office.Interop.Outlook.Application();
    ns = app.GetNamespace("MAPI");
    MessageBox.Show(countRootFolders().ToString());
}

public int countRootFolders()
{
    Microsoft.Office.Interop.Outlook.MAPIFolder rootFolder = this.ns.Session.DefaultStore.GetRootFolder();
    int rootCount = rootFolder.Folders.Count;

    foreach (Microsoft.Office.Interop.Outlook.MAPIFolder subfolder in rootFolder.Folders)
    {
        rootCount += subfolder.Folders.Count;
    }

    return rootCount;
}

Any help greatly appreicated!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:41:03+00:00Added an answer on May 27, 2026 at 6:41 pm

    It looks to me like your loop only asks each of the first level folders how many folders they have. So you get the number of folders in the root, and the number of folders each of those have one level down, but it doesn’t traverse the folder tree to ask further levels how many they have.

    This is a simple tree traversal problem.

    You’ll have to implement a recursive function to traverse down the folder structure to get an accurate count.

    public int countRootFolders(Microsoft.Office.Interop.Outlook.MAPIFolder aFolder)
    {
        int rootCount = aFolder.Folders.Count;
    
        foreach (Microsoft.Office.Interop.Outlook.MAPIFolder subfolder in aFolder.Folders)
        {
            rootCount += countRootFolders( subFolder );
        }
    
        return rootCount;
    }
    

    Call that with the root folder you want to count, and it should be pretty close to what you’re after.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Microsoft.Exchange.WebServices to work with some public folders and their items. I would
I am using Com4J to interact with Microsoft Outlook. I have generated the Java
I have been using Microsoft Outlook and WindowsMobile devices for some years now. But
I'm trying to send an iCal to an outlook, using Java Mail Library, I've
I'm trying to enumerate the properties of an Microsoft.Office.Interop.Outlook.ContactItem object (let's call it ci)
I am receiving HTML newsletter as emails using Microsoft Outlook 2007. There are several
i have developed a add-on for Microsoft outlook email using .net application with visual
When sending an e-mail using Microsoft Outlook I want to be able to send
I'm trying to find a way using the Office.Interop.Outlook COM object to connect to
Using Microsoft SQL Server's database mirroring capability, can the mirroring occur between two clusters?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.