如何获取收件箱文件夹和项目计数?
我终于设法弄清楚如何创建收件箱、已发送项目、日历和邮件的项目计数。联系人,但我在文件夹计数方面遇到问题。
假设我在邮箱中创建了 5 个文件夹,每个文件夹中创建了 2 个子文件夹。当我运行此代码时,它会计算 5 个文件夹,而不是所有文件夹和子文件夹(相当于 15 个文件夹)。
我猜测是 foreach 语句或其他东西,但我仍然是新手:-)
#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
感谢您收到的任何帮助!担
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将需要递归处理以 Namespace.Folders 开头的所有文件夹。
我突然想到:
int allitems = CountFolders(ns.Folders);
...
private int CountFolders(文件夹文件夹)
{
int c = 文件夹.count;
foreach(文件夹中的MAPIFolder文件夹)
{
c += CountFolders(文件夹.文件夹);
}
返回c;
}
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;
}