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

C# 如何指定在哪个文件夹中创建新的Outlook AppointmentItems?,c#,outlook,C#,Outlook,我正在使用以下代码为outlook创建AppointmentItem对象: AppointmentItem apt = (APPointmentItem)OLApp.CreateItem(OlItemType.olAppointmentItem); // set parameters for 'apt', like body, subject etc. // ... apt.Save(); 我有要将此事件放入的日历的名称,但我不知道如何指定新创建的事件应放入哪个文件夹。新事件似乎总是出现在主日

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

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

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

您需要做的是访问该文件夹,然后调用folder.items.add并添加项目。它应该是这样的:

Microsoft.Office.Interop.Outlook.MAPIFolder customer_folder = GetMyFolder();  //function to get your folder
AppointmentItem apt = (APPointmentItem)OLApp.CreateItem(OlItemType.olAppointmentItem);
// set parameters for 'apt', like body, subject etc.
// ...
apt.Save();
customer_folder.Items.Add(apt);

找到了解决办法。尤金·阿斯塔菲耶夫在

因此,上述代码应为:

    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);
    }
使用系统集合;
使用System.Linq;
使用Microsoft.Office.Interop.Outlook;
专用MAPIFolder\u MAPIFolder;
私有MAPIFolder GetMyFolder(字符串路径,IEnumerable文件夹)
{
如果(!path.StartsWith(@“\\”,StringComparison.Ordinal))
返回null;
字符串pathRoot=GetFolderPathRoot(路径);
foreach(folders.Cast().TakeWhile中的文件夹(
文件夹=>\u mapiFolder==null)。选择(
folder=>new{folder,folderRoot=GetFolderPathRoot(folder.FolderPath)})。其中(
文件夹=>folder.folderRoot==pathRoot)。选择(文件夹=>folder.folder))
{
if(folder.DefaultItemType==OlItemType.olAppointmentItem&&folder.FolderPath==path)
{
s_mapiFolder=文件夹;
打破
}
if(folder.Folders.Cast().Any())
GetMapiFolder(false,folder.Folders,path);
}
返回"mapiFolder";;
}
私有静态字符串GetFolderPathRoot(字符串folderPath)
{
//条标题目录分隔符字符
folderPath=folderPath.Remove(0,2);
//查找目录分隔符字符的索引
int index=folderPath.IndexOf(Path.directoryseportorchar,0);
//根据找到的索引重建根路径
返回String.Format(@“\\{0}”,index>0?folderPath.Substring(0,index):folderPath);
}

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

尝试此操作时,出现异常:“无法完成操作。一个或多个参数值无效”。有什么想法吗?…进一步;MAPIFolder::Items是只读属性,这可能就是它抱怨我试图向其中添加内容的原因。brendan,您的代码似乎无法正常工作。也许你可以把它改写成能用的?那太好了!谢谢