Implementing Events in Custom Instance of MailItem interface

J

jadeboer

Hello,

I am attempting to implement the Microsoft.Office.Interop.Outlook.MailItem
interface in an Outlook addin written in C# & Visual Studio 2008 for Outlook
2003; with a version that supports Outlook 2007 to follow.

I can implement the properties of the MailItem interface in my custom class,
but the MailItem's member events such as Close are causing my build to fail.
Any assistance in creating and wiring up these member events would be very
appreciated.

Cheers,
Jon
 
K

Ken Slovak - [MVP - Outlook]

What is failing? How is it failing, and where? You say it's causing the
build to fail, do you get errors when making a build of the addin? What
errors are reported? Please provide more information.
 
J

jadeboer

Hey Ken,

First, thanks for your response.

My new test class implements the MailItem interface, including events and
properties.

The implemented events are such as the following (for use with Outlook 2003):
event ItemEvents_10_SendEventHandler ItemEvents_10_Event.Send
{
add { throw new NotImplementedException(); }
remove { throw new NotImplementedException(); }
}

This event definition will not fail the build. However, when I attempt to
include a handling method and specify it within the event's body, I receive
an error:

// New handling:
event ItemEvents_10_SendEventHandler ItemEvents_10_Event.Send
{
add
{
doSend(object sender, EventArgs e);
//throw new NotImplementedException();
}
remove { throw new NotImplementedException(); }
}

.... the corresponding handler is:
public void doSend(object sender, EventArgs e)
{
// TODO: actually implement
}

Now, the IDE error list states that an add or remove accessor must have a
body, so I guess I'm not thinking about the events and how to handle them in
the proper way.

What I need is to be able to handle the send, etc events of a custom
implementation of a MailItem so as to be able to create and use new types of
messages. Therefore, I need access to the customMailItem, the application so
as to be able to save the item to a folder within the explorer, etc. Any
pointers would be very appreciated.

Cheers Ken,
Jon
 
K

Ken Slovak - [MVP - Outlook]

Not that one method is better than another, but I usually handle item events
something like this:

// at class level
Outlook.ItemEvents_Event _itemEvents = null;

// init code, after instantiating mail item
_itemEvents = (Outlook.ItemEvents_Event)_mail; // _mail == Outlook.MailItem

// hook up the MailItem Close event handler
_itemEvents.Close += new Outlook.ItemEvents_CloseEventHandler(OnItem_Close);
// hook up the MailItem Send event handler

_itemEvents.Send += new Outlook.ItemEvents_SendEventHandler(OnItem_Send);

Removing the events in the shutdown procedure for that Inspector would be
something handled like this:
_itemEvents.Send -= new Outlook.ItemEvents_SendEventHandler(OnItem_Send);

Then, the event hamdlers would look like this:

void OnItem_Send(ref bool Cancel)

{

//MessageBox.Show("MailItem Sending");

}

void OnItem_Close(ref bool Cancel)

{

//MessageBox.Show("MailItem Closing");

}
 
J

jadeboer

Hey Ken,

Very helpful, from what I can see your code looks to be providing a new
event handler for the existing MailItem object's events such as close and
send, versus wiring up events for a custom item class that implements the
MailItem interface. I'll check your idea out.

Thanks again,
Jon
 
K

Ken Slovak - [MVP - Outlook]

Actually, what I usually do is to create lists that contain a wrapper class
for each Explorer or Inspector that gets opened. Each Inspector wrapper
class encapsulates one Inspector and has objects in it for a MailItem or
whatever item type I want to handle. All events are fired directly in that
class, whether for the item or the Inspector itself. That allows me to
handle each Inspector separately and to handle events for that
Inspector/item without conflicting with any other open Inspector.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top