Connect to Outlook calendar from C# using Interop

早过忘川 提交于 2020-01-22 02:35:07

问题


Ok I am trying to connect to an Outlook calendar from C# using the following code:

using Outlook = Microsoft.Office.Interop.Outlook;

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

foreach (Outlook.MAPIFolder subfolder in folder.Folders)
{
    MessageBox.Show(subfolder.Name);
}

However, despite having two Calendars, the piece of code above doesn't see any!

I'm thinking I may have more luck with the below code:

Outlook.MAPIFolder folder = ns.GetFolderFromID("CalendarName", Type.Missing);

But this is throwing the the following exception:

Could not open the item. Try again.

I'm guessing the calendars ID is something other than it's name.

What am I doing wrong?

Also, I'm using C#4 with .Net 4 and Outlook 2010.


回答1:


Are both calendars in the MAPI namespace? What if you loop through the namespaces to see if other ones have a calendar:

Outlook.Application msOutlook = new Outlook.Application();
Outlook.NameSpace session = msOutlook.Session;
Outlook.Stores stores = session.Stores;
foreach (Outlook.Store store in stores)
{
    Outlook.MAPIFolder folder = store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);

    MessageBox.Show(folder.Name);
}


来源:https://stackoverflow.com/questions/9216064/connect-to-outlook-calendar-from-c-sharp-using-interop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!