简体   繁体   English

C#-Outlook-访问新日历

[英]C# - Outlook - get access to a new calendar

I am trying to read all the calendars I have in my outlook with C#, but i have a problem getting access to the ones I create inside outlook (right click -> new calendar). 我正在尝试使用C#读取Outlook中的所有日历,但是我无法访问在Outlook中创建的日历(右键单击->新日历)。

I'm trying to get them by: 我正在尝试通过以下方式获取它们:

Outlook.Application app = new Outlook.Application();
Outlook.NameSpace ns = app.GetNamespace("MAPI");
Outlook.MAPIFolder folderss =   ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);

or by: 或通过:

Application.Session.Stores

but none of them holds my new calendar. 但没有一个人拥有我的新日历。

do you have an Idea how to reach them? 你有一个想法如何到达他们?

Calendars are just Folders with DefaultItemType OlItemType.olAppointmentItem . 日历只是具有DefaultItemType OlItemType.olAppointmentItem Folders
They can be created in any of the Outlook Stores on any level of the Folder hierarchy. 可以在Folder层次结构的任何级别上的任何Outlook Stores创建它们。

Assuming that the calendar was created in the root folder of one of your Stores , the following C# code will find it: 假设日历是在您的一个Stores的根文件夹中创建的,则以下C#代码将找到它:

 void findMyCalendar(String name)
    {
        string path = null;

        Outlook.Application app = new Outlook.Application();
        Outlook.NameSpace ns = app.GetNamespace("MAPI");

        //  there may be more than one Store
        //  each .ost and .pst file is a Store
        Outlook.Folders folders = ns.Folders;

        foreach (Outlook.Folder folder in folders)
        {
            Outlook.MAPIFolder root = folder;
            path = findCalendar(root, name);

            if (path != null)
            {
                break;
            }
        }

        MessageBox.Show(path ?? "not found!");
    }

//  non-recursive search for just one level
public string findCalendar(MAPIFolder root, string name)
    {
        string path = null;

        foreach (Outlook.MAPIFolder folder in root.Folders) 
        {
            if (folder.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase) &&
                (folder.DefaultItemType == OlItemType.olAppointmentItem))
            {
                path = folder.FolderPath;
                break;
            }
        }

        return path;
    } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM