代码之家  ›  专栏  ›  技术社区  ›  Thomi

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

  •  2
  • Thomi  · 技术社区  · 15 年前

    AppointmentItem 具有以下代码的outlook对象:

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

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

    2 回复  |  直到 15 年前
        1
  •  2
  •   Jimi    14 年前

    找到了解决办法。感谢尤金·阿斯塔菲耶夫在 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);
        }
    

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

        2
  •  1
  •   brendan    15 年前

    你需要做的是访问文件夹,然后打电话给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);