Search code examples
c#vstooutlook-addin

Move calendar to another group


I am creating a plugin for Outlook using C# VSTO.

I'm trying to transfer a calendar with all data from one group to another.

Outlook.NameSpace mapiNamespace = Application.GetNamespace("MAPI");
Outlook.CalendarModule calendarModule = (Outlook.CalendarModule)mapiNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar).GetExplorer().NavigationPane.Modules.GetNavigationModule(Outlook.OlNavigationModuleType.olModuleCalendar);
Outlook.NavigationGroups navGroups = calendarModule.NavigationGroups;
Outlook.NavigationGroup source = null;
Outlook.NavigationGroup dest = null;
foreach (Outlook.NavigationGroup navGroup in navGroups)
{
    if (navGroup.Name == "My calendars")
        source = navGroup;
    else if(navGroup.Name == "Shared calendars")
        dest = navGroup;
}
foreach (Outlook.NavigationFolder group in source.NavigationFolders)
{
    dest.NavigationFolders.Add(group);
}

I get the error

"Argument 1: cannot convert from 'Microsoft.Office.Interop.Outlook.NavigationFolder' 'Microsoft.Office.Interop.Outlook.MAPIFolder'".

If I change the line like this,

dest.NavigationFolders.Add((MAPIFolder)group);

I get the error

"System.InvalidCastException: 'Unable to cast a COM object of type "System.__ComObject" to interface type "Microsoft.Office.Interop.Outlook.Outlook.Outlook.MAPIFolder"'". The operation failed because calling QueryInterface of the COM component for the interface with IID "{00063006-0000-...-0000-C000-000000000046}" returned the following error: 'Interface not supported (HRESULT Exception: 0x80004002 (E_NOINTERFACE)).'"

How can I move the calendar with all data from one group to another?


Solution

  • Change the line

    dest.NavigationFolders.Add(group);
    

    to

    dest.NavigationFolders.Add(group.Folder);