在C#中访问Outlook 2010邮件文件夹

fly*_*656 5 c# outlook add-in

我正在开发一个Outlook加载项,最近为了熟悉而切换到C#(我是一个Java人).此时,我只是尝试遍历邮件文件夹并将每条消息的主题打印到控制台,主要是为了确保一切正常工作到目前为止.但是,无论何时运行它,我都会收到以下错误:

无法完成操作.一个或多个参数值无效.

例外文字:

System.ArgumentException:无法完成操作.一个或多个参数值无效.位于Microsoft.Office.Tools的Microsoft.Office.Tools.AddInImpl.OnStartup()的OutlookAddIn2.ThisAddIn.ThisAddIn_Startup(Object sender,EventArgs e)中的Microsoft.Office.Interop.Outlook.NameSpaceClass.GetFolderFromID(String EntryIDFolder,Object EntryIDStore) Microsoft.Office.Tools.AddInBase.Microsoft.Office.Tools中的OutlookAddIn2.ThisAddIn.FinishInitialization()中的Microsoft.Office.Tools.AddInBase.OnStartup()中的.AddInImpl.AddInExtensionImpl.Microsoft.Office.Tools.EntryPoint.OnStartup() Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.ExecuteCustomization.Microsoft.VisualStudio.Tools.Office.Runtime中的Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.ExecuteCustomization.ExecutePhase(ExecutionPhases executionPhases)中的.EntryPoint.FinishInitialization(). .Interop.IExecuteCustomization2.ExecuteEntryPoints()

加载的装配:

我对此感到有些困惑,因为这是Microsoft在MSDN上推荐的用户选择文件夹的精确方法.我已经包含了我的来源,如果您有任何想法,请告诉我.感谢您花时间阅读这篇文章,并愿意提供帮助!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;

namespace OutlookAddIn2
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            //Get application namespace and grab the original folder object
            Outlook.Folder pickFolder = (Outlook.Folder)Application.Session.PickFolder();

            //Outlook.Folder mrFolder = Application.Session.GetFolderFromID(pickFolder.EntryID, pickFolder.StoreID) as Outlook.Folder;

            foreach (Outlook.MailItem oMailItem in pickFolder.Items)
            {
                Console.WriteLine(oMailItem.Subject);
            }
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #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
    }
}
Run Code Online (Sandbox Code Playgroud)

Sli*_*SFT 0

pickFolder.EntryID您应该调试或添加跟踪语句以查看和的值pickFolder.StoreID。如果没有有效的EntryIDStoreID就会抛出这个错误。

Trace.TraceInformation("EntryID: {0}\tStoreID: {1}", pickFolder.EntryID, pickFolder.StoreID);
Run Code Online (Sandbox Code Playgroud)

pickFolder如果用户单击取消按钮,您应该检查是否为空。

另外,如果您让用户选择一个文件夹,则无需通过再次选择该文件夹GetFolderFromID- 您已经有对其的引用。