我正在开发一个Outlook加载项,它可以通过两种方式之一工作,具体取决于用户的选择 - 它可以处理选定的电子邮件,或者处理所选文件夹中的所有电子邮件。我已经完成了第一部分工作,但第二部分给了我麻烦,可能是因为我只是错误地调整了第一部分的代码。我相信问题归结为在C#Outlook加载项中正确获取当前选定的文件夹。顺便说一下,我正在使用.NET 3.5和Outlook 2007。
首先,电子邮件代码 - 如果用户在其收件箱中选择了一封或多封电子邮件,并使用“选定的电子邮件”选项运行我的插件,则运行以下代码(并且工作正常!):
public static void processSelectedEmails(Outlook.Explorer explorer)
{
//Run through every selected email
for (int i = 1; i <= explorer.Selection.Count; i++)
//alternatively, foreach (Object selectedObject in explorer.Selection)
{
Object selectedObject = explorer.Selection[i];
if (!(selectedObject is Outlook.Folder))
{
string errorMessage = "At least one of the items you have selected is not an email.";
//Code for displaying the error
return;
}
else
Outlook.MailItem email = (selectedObject as Outlook.MailItem);
//Do something with current email
}
}
如果用户在Outlook中转到导航窗格(默认情况下在左侧),选择文件夹或子文件夹(可能是收件箱,已发送邮件或其他文件夹),我已尝试调整此代码以执行其他操作已创建)。然后,用户可以在我的加载项中选择“处理选定文件夹”选项,该选项与上面的代码基本相同,但处理所选文件夹中的所有电子邮件。我已将其设置为仅在用户选择了单个文件夹时才起作用。
public static void processFolder(Outlook.Explorer explorer)
{
//Assuming they have selected only one item
if (explorer.Selection.Count == 1)
{
//Make sure that that selected item is a folder
Object selectedObject = explorer.Selection[1];
if (!(selectedObject is Outlook.Folder))
{
string errorMessage = "The item you have selected is not a folder.";
//Code for displaying the error
return;
}
//Code for running through every email in that folder
}
}
我还没有编写实际运行所选文件夹中所有电子邮件的代码,因为我的代码永远不会超过if (!(selectedObject is Outlook.Folder))
。即使最近选择的项目是您的收件箱,我也会收到我在此时编程的错误。也许我在滥用探险家。选择的事情?任何帮助将不胜感激。
这对回答我的问题很重要 - 加载项有一个名为“explorer”的字段,它在启动时生成:explorer = this.Application.ActiveExplorer
。这是传递给我的函数的“资源管理器”,以便他们知道所选内容。正如我所说,这适用于选定的电子邮件,但不适用于选定的文件夹。任何见解将不胜感激!
编辑1:看来这个问题基本上与Get all mails in outlook from a specific folder重复,但没有答案。
编辑2:我一直在做进一步的研究,似乎我可以通过使用{创建弹出窗口来选择文件夹来获得几乎相同的功能(但遗憾的是还有一个步骤) {1}}方法。有没有办法根据当前选择的文件夹执行此操作,而不是强制用户选择一个新文件夹?
编辑3:我修改了此处的代码:http://msdn.microsoft.com/en-us/library/ms268994(v=vs.80).aspx以进一步显示哪些内容无法正常运行:
Application.Session.PickFolder()
无论是选择邮件,还是转到导航窗格并选择文件夹,我都会收到消息“此项目是电子邮件”。
答案 0 :(得分:4)
Explorer.Selection
仅适用于Items
( MailItem,AppointmentItem等) - 不是Folders
。要访问当前选定的Folder
,您需要Explorer.CurrentFolder
。
Folder.Items
可让您访问给定Items
中的所有Folder
。