Create a gist now

Instantly share code, notes, and snippets.

Sample Outlook Add-in code to enumerate items
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
namespace ConnectSampleAddin
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
EnumAllFoldersInStore();
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
private void EnumAllFoldersInStore()
{
Outlook.Folder rootFolder = this.Application.Session.DefaultStore.GetRootFolder() as Outlook.Folder;
EnumFolder(rootFolder);
}
private void EnumFolder(Outlook.Folder folder)
{
Outlook.Items allFolderItems = folder.Items;
if (allFolderItems.Count > 0) // If we have any items in this folder
{
for (int i = 1; i < allFolderItems.Count; i++) // Index starts at 1 for Items collection
{
object folderItem = allFolderItems[i];
// If item is a MailItem, cast it to a MailItem.
// You should have similar code for ContactItem, etc. etc.
if (folderItem is Outlook.MailItem)
{
Outlook.MailItem mailItem = folderItem as Outlook.MailItem;
// Get PR_MESSAGE_CLASS property
object prMsgClass = mailItem.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x001A001E");
string msgClass = prMsgClass.ToString();
// Determine if the item is an Enterprise Vault item
if (msgClass.Contains("IPM.Note.EnterpriseVault.Shortcut"))
MessageBox.Show("Found an Enterprise Vault item.");
else
MessageBox.Show("This is not an Enterprise Vault item.");
}
}
}
// Enum child folders of this one
Outlook.Folders childFolders = folder.Folders;
if(childFolders.Count > 0)
{
foreach (Outlook.Folder childFolder in childFolders)
EnumFolder(childFolder);
}
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment