Items ItemAdd Outlook 2007 problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I wan't to have something like this:

I add items to Items collection and I do not wan't to handle the event (at
startup) and after I add some of those items I wan't to start handling the
ItemAdd event.

My code looks like this:

class Folder
{
private Outlook.Folder _folder = null;

private Outlook.Items _items = null;

private Logic.ContactsLogic _contactsLogic = null;

public Folder(Outlook.NameSpace outlook, string folderName)
{
Outlook.Folder defaultCalendarFolder =
(Outlook.Folder)outlook.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);

_folder = GetFolder(defaultCalendarFolder, folderName);

_items = _folder.Items;

_contactsLogic = new ContactsLogic();

// In here I add some items to the folder
_contactsLogic.SynchronizeContacts(this);

HookUpEvents();
}


private void HookUpEvents()
{
_folder.BeforeFolderMove += new
Microsoft.Office.Interop.Outlook.MAPIFolderEvents_12_BeforeFolderMoveEventHandler(Folder_BeforeFolderMove);
_folder.BeforeItemMove += new
Microsoft.Office.Interop.Outlook.MAPIFolderEvents_12_BeforeItemMoveEventHandler(Folder_BeforeItemMove);
_items.ItemAdd += new
Microsoft.Office.Interop.Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);
_items.ItemChange += new
Microsoft.Office.Interop.Outlook.ItemsEvents_ItemChangeEventHandler(Items_ItemChange);

_eventsHookedUp = true;
}
}

Although I add event handlers after I add the Items to the collection (in
statement _contactsLogic.SynchronizeContacts(this); ) I still get to handle
them in Items_ItemAdd and I don't wan't to
 
Is your call to SynchronizeContacts synchronous or asynchronous? If it's
waiting to return until the synching is finished you should not get ItemAdd
events.
 
It's normal synchronous call :/

Maybe the problem is that it all hapens in the constructor?
 
Luk said:
It's normal synchronous call :/

Maybe the problem is that it all hapens in the constructor?

No it isn't, even when I make HookUpEvents public and call it after Folder
object is constructed events still fire
 
If the synch is finished when you add the event handlers no events from the
synch could possibly occur. It's not finished at the point where you're
adding the event handlers. That's my bet.
 
funny thing...

_contactsLogic.SynchronizeContacts(this);
System.Windows.Forms.Application.DoEvents();
HookUpEvents();

Like this it works ok, events are queued in some way and fire as soon as
there is no other code executing?
 
It looks like there might have been some problem in when the synch code was
releasing its unmanaged COM objects possibly. The garbage collector runs
asynchronously and that DoEvents might buy enough time for the garbage
collector to have released the objects.

If the synch code is working with COM objects are you calling
Marshal.ReleaseComObject on each one plus calling the garbage collector
explicitly and waiting for finalization before exiting the synch code?
 
Back
Top