[英]Accessing Outlook 2010 Mail Folder in C#
我正在开发一个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
}
}
托盘:
public static Folder FOLDER_1;
public static Folder FOLDER_2;
public static Folder FOLDER_N;
/// <summary>
/// Hilo que lee el archivo de datos PST del OUTLOOK
private static void readPst()
{
try
{
Application app = new Application();
NameSpace outlookNs = app.GetNamespace("MAPI");
MAPIFolder mf = outlookNs.GetDefaultFolder(OlDefaultFolders.olFolderTasks);
string names = mf.FolderPath.Split('\\')[2];
Folder fMails = getFolder(fCarpetasPersonales.Folders, "Inbox");
FOLDER_1= getFolder(fMails.Folders, "FOLDER_1");
FOLDER_2= getFolder(fMails.Folders, "FOLDER_2");
FOLDER_N= getFolder(fMails.Folders, "FOLDER_n");
//TO DO... For example: foreach (object item in fMails.Items)
private static Folder getFolder(Folders folders, string folder)
{
foreach (object item in folders)
{
if (item is Folder)
{
Folder f = (Folder)item;
if (f.Name.Equals(folder))
{
return f;
}
}
}
return null;
}
您应该调试或添加trace语句以查看pickFolder.EntryID
和pickFolder.StoreID
的值。 没有有效的EntryID
或StoreID
,它将抛出此错误。
Trace.TraceInformation("EntryID: {0}\tStoreID: {1}", pickFolder.EntryID, pickFolder.StoreID);
如果用户单击取消按钮,您应该检查pickFolder
是否为null。
此外,如果您让用户选择一个文件夹,则无需再通过GetFolderFromID
选择该文件夹 - 您已经有了对它的引用。
当然,如果你想要Inbox文件夹,你可以尝试这个吗?
Outlook.Application app = new Outlook.Application();
Outlook.NameSpace ns = app.GetNamespace("MAPI");
Outlook.Folder folder = app.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;
目前尚不清楚你是否通过@SilverNinja指出调试器中的代码。 确保StoreID和EntryID有效非常重要。
还有一些可能性:
您的Outlook PST略有损坏。 尝试scanPST,看看是否有帮助。
另外,你会认为pickFolder枚举足够智能,跳过这些,但你在文件夹树的顶层还有其他项目而不是文件夹吗? 我在枚举联系人和在我的联系人文件夹中包含非联系人项目时遇到了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.