Mapifolder Events beforeitemmove

Nov 20, 2013 at 12:00 PM
Hi out there,

I've just begun to port an Outlook-Addin Project to NetLOffice. It seems as if only one single event creates problems:
private Outlook.MAPIFolder olFold;
private Outlook.MAPIFolderEvents_12_Event Calendarevents;
// [...]

olFold = OlNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
Calendarevents = (Outlook.MAPIFolderEvents_12_Event)olFold;

CalendarEvents.BeforeItemMove += new Outlook.MAPIFolderEvents_12_BeforeItemMoveEventHandler(Calendarevents_BeforeItemMove);
The idea is obviously to create an eventhandler for the beforeitemmove-Event of the Calendar of the user.

I do not find a way to port this last line to a usage with netoffice

Can someone help me out of that?
Nov 20, 2013 at 12:34 PM
In NO, any event ends with "Event" because some office classes use methods and events with the same name. In the Interop assemblies, you have to convert the instance to the event interface to trigger the event and you got a compiler warning if you call the method with same name. This is because NO use "Event" to avoid this disadvantages.

Nov 20, 2013 at 1:01 PM
Hi Sebastian,

ok, ty, I was looking at the MapiFolder-Object, instead of the Folder-Object.

If someone is interested:
private Outlook.Folder olFold;

olFold.BeforeItemMoveEvent += olFold_BeforeItemMoveEvent;


Nov 25, 2013 at 8:45 AM
Hi Sebastian,

Netoffice throws an Error at runtime when I try to convert Mapifolder to Folder
olMapiFold = (Outlook.MAPIFolder)OlNs.GetDefaultFolder(Outlook.Enums.OlDefaultFolders.olFolderCalendar);
olFold = (Outlook.Folder)olMapiFold; //generates Error 
" Cannot cast 'this.olMapiFold' (which has an actual type of 'NetOffice.OutlookApi.MAPIFolder') to 'NetOffice.OutlookApi.Folder"

AFAIR this is possible in Outlook.
Nov 25, 2013 at 10:08 AM
hello andreas,

MAPIFolder doesnt inherites from Folder in NetOffice. This is may a heayy issue. (not sure why because NO is alway aware for the class/interface hierarchy)
i have to do more research for the problem but i'm currently on my workplace. i want clearify the situation at home asap.

Nov 25, 2013 at 10:16 AM
Edited Nov 25, 2013 at 10:17 AM
Update: The OLE/COMObject Viewer tolds me MAPIFolder doesnt inherites from Folder(and otherwise) and i'm wondering you mean this must works fine. At the moment for me: this can't be work. i want imitate the scenario in Interop/VSTO at home.

Nov 26, 2013 at 6:48 AM
Hi Sebastian,

sorry, I guess I did not make me clear (and the last sentence of my last post is rubbish):
In Microsoft.Office.Interop.Outlook the Mapifolder-Object has the event BeforeItemMove (See code below).
public partial class ThisAddIn
        private Outlook.NameSpace OlNs;
        private Outlook.MAPIFolder olFold;
        private Outlook.MAPIFolderEvents_12_Event CalendarEvents;
        private Outlook.Items CalendarItems;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
            OlNs = Globals.ThisAddIn.Application.GetNamespace("MAPI");
            if (OlNs != null)
                olFold = OlNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);

                if (olFold != null)
                    CalendarEvents = (Outlook.MAPIFolderEvents_12_Event)olFold;
                    CalendarItems = olFold.Items;

                    if (CalendarItems != null)
                        CalendarEvents.BeforeItemMove += CalendarEvents_BeforeItemMove;

        void CalendarEvents_BeforeItemMove(object Item, Outlook.MAPIFolder MoveTo, ref bool Cancel)
            throw new NotImplementedException();
Checking the events in Netoffice after your first reply, I saw that in Netoffice the Folder-Object has the event BeforeItemMove (In Microsoft.Office.Interop.Outlook the Mapifolder-Object has that event), while the MapiFolder-Object has not.

The GetDefaultFolder-Method (as well as the GetFolderFromID-Method) of the Namespace-Object in Netoffice returns a MapiFolder-Object.

Now: If it is not possible to cast a Mapifolder-Object into a Folder-Object, how can I trigger these events at Addin_Startup?


Nov 26, 2013 at 10:18 PM
Edited Nov 27, 2013 at 2:57 AM
hey andreas,

i do some tests and folder inherites from mapi folder. this means the up-cast mapi folder to folder has to work but its failed. the result proxy in NO was identified as MapiFolder instance(interface id:00063006-0000-0000-c000-000000000046) and not as Folder instance. NO creates the corresponding mapifolder wrapper for this reason. this is the first time i see this kind of problem. normaly is NO indentifier the correct proxy type, creates the wrapper and downcast the wrapper result value automaticly. i have to do further research to see how i can unmask the proxy instance.

as hotfix i suggest you to use a replace technique like this:

example: replace an NO wrapper instance and keep com proxy management alive

// get mapi folder
Outlook.MAPIFolder mapiFolder = outlookNS.GetDefaultFolder(OlDefaultFolders.olFolderInbox);

// replace mapiFolder with a replace .ctor
Outlook.Folder folder = new Outlook.Folder(mapiFolder);

// mapiFolder is not valid any longer
mapiFolder = null;

// folder is now a child from ex-mapiFolder parent and contains also all childs from mapiFolder in NO proxy management(no childs in this example)


(btw: is use outlook 2007 for tests, the minimum version to use Folder. whats your current version?)
Update: i find a solution for the problem and change the implementation in upcoming 1.7 release.
Nov 28, 2013 at 7:59 AM
Hi Sebastian,

thank you for your efforts, the code works.

Here we use both Office 2007 and 2010 (and next year additionally Office 2013 I guess). Which was one of the reasons why I jumped on the Netoffice-Train. xD
I'm developing with Office 2010, but I publish Outlook-Addins from an environment with Outlook 2007 installed (which usually works fine for both versions)