Outlook 2003 Appointments -- drag and drop event using C#

  • Thread starter Thread starter Curtis Justus
  • Start date Start date
C

Curtis Justus

Hi everybody,

I have created an add-in where I need to capture a drag and drop event of an
appointment in a user's calendar. I used the ItemLoad event in the 2007
libraries, but that doesn't exist in 2003.

I have attached to the Items.ItemChange event of the calendar folder
(olFolderCalendar), but that never fires. For that matter, neither does the
ItemAdd or ItemRemove events.

I have my inspectors hooked up, but they never fire because an inspector is
not created on a drag-drop of an appointment.

Would anybody have any ideas of something I can try?

Thanks for your help,
cj
 
If you add a new item to the calendar folder, not with drag and drop but
Actions, New Appointment does ItemAdd() fire at all? If not then it's
something in how you're creating or scoping your event handler.

Is the folder object and items collection declared at a class or other scope
level that will keep them alive once your procedure where you add the event
handler ends?

Show the code you use to create the event handlers.
 
Hello Ken,

If I hook up the ItemAdd event, it does not fire if I add anything -- even
without the drag and drop.

Here is the code I am using...

public partial class ThisAddIn
{
private Outlook.Application outApp;
private Outlook.NameSpace _nameSpace;
private Outlook.MAPIFolder _calendarFolder;

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
outApp = this.Application;

_nameSpace = outApp.GetNamespace("MAPI");

_calendarFolder =
_nameSpace.GetDefaultFolder(OlDefaultFolders.olFolderCalendar);

_calendarFolder.Items.ItemAdd += new
ItemsEvents_ItemAddEventHandler(Items_ItemAdd);
}

void Items_ItemAdd(object Item)
{
MessageBox.Show("Item Added");
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
_calendarFolder.Items.ItemAdd -= new
ItemsEvents_ItemAddEventHandler(Items_ItemAdd);
outApp = null;

}
}

Thanks for your help...
cj
 
Your Items collection is going out of scope as soon as that procedure ends.
Then it gets garbage collected and your event handlers then fail to fire.

Your class level declarations section should include a declaration for an
Items collection:

private Outlook.Items _items;

Assign that to your calendar folder's items collection and add the event
handler to that Items collection and the events should start to fire.
 
VOILA!

Ken... you got me thinking about the scope. Since I wasn't referring to the
Items collection directly in my definition, I thought maybe something was
getting disconnected in the Interop with the com layer. I set up a class
level variable:

private Items _calendarItems;

Then, I put the following code in the startup:

_calendarItems.ItemChange += new
ItemsEvents_ItemChangeEventHandler(_calendarItems_ItemChange);

Lo and behold, the change event fires -- even on the drag and drop.

Thanks for pointing me in the right direction...
cj
 
You always have to be more aware of scoping and possible garbage collection
in managed code than in unmanaged code. You'd be surprised at how many
problems are solved just by thinking about and following scoping rules.

Good, I'm glad things are working now.
 
Back
Top