gpt4 book ai didi

c# - 如何指定在哪个文件夹中创建新的 Outlook AppointmentItems?

转载 作者:行者123 更新时间:2023-11-30 22:43:44 24 4
gpt4 key购买 nike

我正在使用以下代码为 outlook 创建 AppointmentItem 对象:

AppointmentItem apt = (APPointmentItem)OLApp.CreateItem(OlItemType.olAppointmentItem);
// set parameters for 'apt', like body, subject etc.
// ...
apt.Save();

我有一个日历的名称,我想将此事件放入其中,但我不知道如何指定新创建的事件应放入哪个文件夹。新事件似乎总是出现在主日历文件夹中。

最佳答案

找到解决方案。感谢 Eugene Astafiev 在 http://www.add-in-express.com/creating-addins-blog/2011/11/04/outlook-create-appointment-item/ 提供它

所以上面的代码应该是:

    using Microsoft.Office.Interop.Outlook;

Application outlookApplication = new Application();
MAPIFolder customer_folder = GetMyFolder(path, outlookApplication.Session.Folders); //function to get your folder
AppointmentItem apt = (AppointmentItem)customer_folder.Items.Add(OlItemType.olAppointmentItem);
// set parameters for 'apt', like body, subject etc.
// ...
apt.Save();

下面是 GetMyFolder 递归查找自定义文件夹的代码:

    using System.Collections;
using System.Linq;
using Microsoft.Office.Interop.Outlook;

private MAPIFolder _mapiFolder;

private MAPIFolder GetMyFolder(string path, IEnumerable folders)
{
if (!path.StartsWith(@"\\", StringComparison.Ordinal))
return null;

string pathRoot = GetFolderPathRoot(path);

foreach (Folder folder in folders.Cast<Folder>().TakeWhile(
folder => _mapiFolder == null).Select(
folder => new { folder, folderRoot = GetFolderPathRoot(folder.FolderPath) }).Where(
folder => folder.folderRoot == pathRoot).Select(folder => folder.folder))
{
if (folder.DefaultItemType == OlItemType.olAppointmentItem && folder.FolderPath == path)
{
s_mapiFolder = folder;
break;
}

if (folder.Folders.Cast<Folder>().Any())
GetMapiFolder(false, folder.Folders, path);
}

return _mapiFolder;
}

private static string GetFolderPathRoot(string folderPath)
{
// Strip header directory seperator characters
folderPath = folderPath.Remove(0, 2);

// Find the index of a directory seperator character
int index = folderPath.IndexOf(Path.DirectorySeparatorChar, 0);

// Reconstruct the root path according to the index found
return String.Format(@"\\{0}", index > 0 ? folderPath.Substring(0, index) : folderPath);
}

编辑:修改代码以仅在指定路径根目录下的文件夹中递归搜索。

关于c# - 如何指定在哪个文件夹中创建新的 Outlook AppointmentItems?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3800627/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com