问题
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