Reputation: 385
I finally managed to work out how to create item counts of the inbox, sent items, calendar & contacts but I'm having problems with the folder count.
Let say I have 5 folders created in my mailbox and 2 sub folders in each folder. When I run this code, it counts 5 folders instead of all folders and sub folders which would equal 15 folder.
I'm guessing a foreach statement or something but I still a newb :-)
#region Run Item Count
Microsoft.Office.Interop.Outlook.Application app = null;
Microsoft.Office.Interop.Outlook._NameSpace ns = null;
private void btnRunItemCount_Click(object sender, EventArgs e)
{
app = new Microsoft.Office.Interop.Outlook.Application();
ns = app.GetNamespace("MAPI");
MAPIFolder oInbox = this.ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
tbInboxItemCount.Text = oInbox.Items.Count.ToString();
MAPIFolder oSentItems = this.ns.GetDefaultFolder(OlDefaultFolders.olFolderSentMail);
tbSentMailItemCount.Text = oSentItems.Items.Count.ToString();
MAPIFolder oCalendar = this.ns.GetDefaultFolder(OlDefaultFolders.olFolderCalendar);
tbCalendarItemCount.Text = oCalendar.Items.Count.ToString();
MAPIFolder oContacts = this.ns.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
tbContactsItemCount.Text = oContacts.Items.Count.ToString();
MAPIFolder oFolder = this.ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
tbFolderItemCount.Text = oInbox.Folders.Count.ToString();
}
#endregion
Thanks for any help received! Dan
Upvotes: 2
Views: 2877
Reputation: 66356
You will need to recursively process all folders starting with Namespace.Folders. Off the top of my head:
int allitems = CountFolders(ns.Folders);
...
private int CountFolders(Folders folders)
{
int c = folders.count;
foreach (MAPIFolder folder in folders)
{
c += CountFolders(folder.Folders);
}
return c;
}
Upvotes: 2