ItemSend doesn't work

M

Mike Belshe

I've got an outlook add-in. I'm running Outlook XP, .NET 1.1. My
Add-In is written in C#.

I'm trying to trap either the Application.ItemSend or the
MailItem.ItemSend event to put up a dialog and possibly let the user
cancel the send. I can get the events to fire, but setting the Cancel
flag doesn't work. I set Cancel to true, and the message is sent
anyway.

Here is the simple code. Again, my event handler does get called- I can
trap a breakpoint there. But, the cancel just doesn't work.


// this runs on startup, app is the application object
// handed to me by the extensibility interface
{
Outlook.Application myApp = (Outlook.Application)app;
myApp.ItemSend += new
Outlook.ApplicationEvents_10_ItemSendEventHandler(olApp_ItemSend);
}

private void olApp_ItemSend(object Item, ref bool Cancel)
{
Cancel = true;
}

Thanks!
Mike
 
M

Mike Belshe

I found a good KB article on a problem similar to this in other outlook
apps. Its at http://support.microsoft.com/?id=830519. I think its the
same problem, although the KB article refers to Word and Excel rather
than Outlook.

I tried applying the fix in the KB article, and my custom sink helper
just never gets called. I have no idea why. Pretty sure I did it
right.

Anyone successful with this and Outlook?

Thanks,
Mike

Here is my ApplicationEventSinkHelper object:


using System;
using System.Runtime.InteropServices;
using Outlook; // The default name for a custom Word IA.
// If you use Office XP and you have the PIAs
// referenced in your project, you must change the previous line to
read:
// using Word = Microsoft.Office.Interop.Word;

//
// The following helper code attempts to work around a microsoft bug in
pre-Outlook2003.
// The problem is that the Cancel argument doesn't properly pass back in
the
// EventSend handler. See http://support.microsoft.com/?id=830519 for
details.
//
namespace OutlookApplicationEvents
{
/// <summary>
/// Hand-grown definition of the ItemEvents interface.
/// </summary>
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch),
GuidAttribute("0006304E-0000-0000-C000-000000000046")]
public interface DOutlookApplicationEvents
{
[DispId(0x0000f002)] void ItemSend(object Item, ref bool Cancel);
[DispId(0x0000f003)] void NewMail();
[DispId(0x0000f004)] void Reminder(object Item);
[DispId(0x0000f005)] void OptionsPagesAdd(Outlook.PropertyPages
Pages);
[DispId(0x0000f006)] void Startup();
[DispId(0x0000f007)] void Quit();
}

/// <summary>
/// Custom handler to help sync the events
/// </summary>
public class OutlookAppEventHelper : DOutlookApplicationEvents,
IDisposable
{
/// <summary>
/// ctor
/// </summary>
public OutlookAppEventHelper()
{
m_oConnectionPoint = null;
m_Cookie = 0;
}

public void ItemSend(object Item, ref bool Cancel)
{
System.Diagnostics.Debug.WriteLine("Got called");
}
public void NewMail()
{
System.Diagnostics.Debug.WriteLine("Got called");
}
public void Reminder(object Item)
{
System.Diagnostics.Debug.WriteLine("Got called");
}
public void OptionsPagesAdd(Outlook.PropertyPages Pages)
{
System.Diagnostics.Debug.WriteLine("Got called");
}
public void Startup()
{
System.Diagnostics.Debug.WriteLine("Got called");
}
public void Quit()
{
System.Diagnostics.Debug.WriteLine("Got called");
}




private UCOMIConnectionPoint m_oConnectionPoint;
private int m_Cookie;

/// <summary>
/// Setup a connection
/// </summary>
/// <param name="obj"></param>
public void SetupConnection(Outlook.Application obj)
{
if (m_Cookie != 0) return;

// GUID of the DIID_ItemEvents dispinterface.
Guid guid = new Guid("{0006304E-0000-0000-C000-000000000046}"); //
events

// QI for IConnectionPointContainer.
UCOMIConnectionPointContainer oConnPointContainer =
(UCOMIConnectionPointContainer)obj;

// Find the connection point and then advise.
oConnPointContainer.FindConnectionPoint(ref guid, out
m_oConnectionPoint);
m_oConnectionPoint.Advise(this, out m_Cookie);
}

/// <summary>
/// tear down a connection
/// </summary>
public void RemoveConnection()
{
if (m_Cookie != 0)
{
m_oConnectionPoint.Unadvise(m_Cookie);
m_oConnectionPoint = null;
m_Cookie = 0;
}
}

/// <summary>
/// Dispose
/// </summary>
public void Dispose()
{
RemoveConnection();
}

}
}
 

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