_ItemSend Cancel

J

John

Hi
I'm writing a OutLook Add-in using VS 2005, C# and VSTO. In the Application
_ItemSend event I do a check for number of recipients and then if more then
one I show the user a message box and set cancel to true. The message stays
open and the user can remove the extra email address and then click send.
This second time the Application_ItemSend event code is not called. Do I need
to do something else to get the event to fire after cancel is set to true?

Thanks John
 
K

Ken Slovak - [MVP - Outlook]

Do you have an Application object declared at a scope level such that it
won't be garbage collected? What about your event handlers, could they be
garbage collected? I usually define an Application object as public to my
ThisAddIn class and assign event handlers for Application and NameSpace
events events in Startup(). That way those handlers and the object are not
garbage collected until I release them in Shutdown().
 
J

John

Hi Ken

Thanks for the reply and suggestion. I didn't have a Application object at
all. I was using the ActiveExplorer() for the calls to create menu bars and
buttons. I was also not creating the ItemSend event in Startup. I have
changed both of these to look like this:

using Application=Microsoft.Office.Interop.Outlook.Application;

private Application oApplication;

private void ThisApplication_Startup(object sender, EventArgs e)
{
oApplication = new Application();
...
oApplication.ItemSend += new
ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
}

This works in so far as I get the Send Event called the second time but
while in

private void Application_ItemSend(object Item, ref bool Cancel)
{
MailItem MailMessage = (MailItem)Item;
try
{
if (MailMessage.Recipients.Count == 1)
{

}
Event I get a message dialog that states: A program is trying to accesss
e-mail address...

This message is new sense I move the application object to be public in the
class. How do I stop this message from show?

Thanks John
 
K

Ken Slovak - [MVP - Outlook]

If you create a new Application object instead of using the one passed to
you in Startup() which is derived from the application object passed in
OnConnection you get the security prompts. You want to derive your object
from the safe Application object passed to you.

Use this.Application to instantiate your object:

oApplication = this.Application;
 

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