【问题标题】:Create task in specific mailbox from Outlook vsto add-in从 Outlook vsto 加载项在特定邮箱中创建任务
【发布时间】:2021-11-27 21:16:11
【问题描述】:
我正在尝试使用 Outlook vsto 插件创建任务。任务正在默认文件夹中创建,但我想将其保存到另一个邮箱文件夹。从this 帖子中,我了解了如何获取默认文件夹,但我没有得到用于设置文件夹路径的属性。
Outlook.TaskItem t = this.Application.CreateItem(Outlook.OlItemType.olTaskItem) as Outlook.TaskItem;
if (t != null)
{
t.Subject = "Hello, Reader";
// what attribute to refer (t.) to set the folder path
t.Save();
}
【问题讨论】:
标签:
c#
vsto
outlook-addin
【解决方案1】:
有几种方法可以完成工作:
- 您可以使用
CreateItem 方法,然后调用Move 将新创建的文件夹移动到所需的文件夹:
using System.Runtime.InteropServices;
// ...
private void CreateTaskItemUsingCreateItem(Outlook._Application Application)
{
Outlook.TaskItem task = Application.CreateItem(Outlook.OlItemType.olTaskItem)
as Outlook.TaskItem;
if (task != null)
{
task.Subject = "change oil in the car";
task.Save();
//task.Display(false);
task.Move(folder);
Marshal.ReleaseComObject(task);
}
}
- 您可以考虑使用
Items 类的Add 方法创建任务项的另一种方法。此方法还接受 OlItemType 枚举并返回一个新创建的 Outlook 项目。
using System.Runtime.InteropServices;
// ...
private void CreateTaskItemUsingItemAdd(Outlook._Application Application)
{
Outlook.NameSpace ns = null;
Outlook.MAPIFolder taskFolder = null;
Outlook.Items items = null;
Outlook.TaskItem task = null;
try
{
ns = Application.GetNamespace("MAPI");
taskFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks);
items = taskFolder.Items;
task = items.Add(Outlook.OlItemType.olTaskItem) as Outlook.TaskItem;
task.Subject = "change oil in the car";
task.Save();
task.Display(false);
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
finally
{
if (task != null) Marshal.ReleaseComObject(task);
if (items != null) Marshal.ReleaseComObject(items);
if (taskFolder != null) Marshal.ReleaseComObject(taskFolder);
if (ns != null) Marshal.ReleaseComObject(ns);
}
}
因此,基本上,在任何 Outlook 文件夹中,您都可以获取 Items 类的实例并使用 Add 方法创建所需项目类型的新实例。
更多信息请参见How To: Create a new Task item in Outlook。
【解决方案2】:
不使用Application.CreateItem,而是打开目标MAPIFolder对象并调用MAPIFolder.Items.Add