想要枚举Outlook文件夹

Mat*_*att 4 .net c# vb.net outlook

我正在寻找一些代码(首选C#或VB.NET)来迭代Outlook邮箱中的所有文件夹并返回这些文件夹的名称.我不打算弹出Outlook文件夹对话框,而是从Outlook外部返回给定邮箱中的文件夹名称.

谢谢

For*_*ker 7

使用VSTO(Visual Studio Tools for Office)实际上非常容易.首先使用VSTO创建一个Outlook 2007添加.以下是我的一些实验性代码.

   private void RecurseThroughFolders(Outlook.Folder theRootFolder, int depth)
    {
        if ( theRootFolder.DefaultItemType != Outlook.OlItemType.olMailItem ) {
            return;
        }

        Console.WriteLine("{0}", theRootFolder.FolderPath);

        foreach( Object item in theRootFolder.Items ) {
            if (item.GetType() == typeof( Outlook.MailItem )) {
                Outlook.MailItem mi = (Outlook.MailItem)item;
                if (mi.Categories.Length > 0) {
                    WriteLinePrefix(depth);
                    Console.WriteLine("  $ {0}", mi.Categories);
                }
            }
        }

        foreach (Outlook.Folder folder in theRootFolder.Folders) {
            RecurseThroughFolders(folder, depth + 1);
        }
    }

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    Outlook.Application olApp = new Outlook.Application();

    Console.WriteLine("Default Profile = {0}", olApp.DefaultProfileName);

    Console.WriteLine("Default Store = {0}", olApp.Session.DefaultStore.DisplayName);

    selectExplorers = this.Application.Explorers;
    selectExplorers.NewExplorer += new Outlook.ExplorersEvents_NewExplorerEventHandler( newExplorer_Event );

    Outlook.Folder theRootFolder  = (Outlook.Folder) olApp.Session.DefaultStore.GetRootFolder();
    RecurseThroughFolders( theRootFolder, 0 );
}
Run Code Online (Sandbox Code Playgroud)