The following steps works fine at my side.
- Create a Shared Add In. Choose Outlook to be the supported Application.
- In the Application Property page, set Outlook to be the start up program.
- Add a reference to Microsoft Outlook 11.0 Object Library.
Import the namespace:
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Windows.Forms;
5.Replace the original system generated fields:
private object applicationObject;
private object addInInstance;
with the following new fields: (No ItemSend event)
public Outlook.Application OutlookApplication;
public Outlook.Inspectors OutlookInspectors;
public Outlook.Inspector OutlookInspector;
public Outlook.MailItem OutlookMailItem;
6.In the OnConnection method, replace all the system generated codes with the following ones:
OutlookApplication = application as Outlook.Application;
OutlookInspectors = OutlookApplication.Inspectors;
OutlookInspectors.NewInspector += new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(OutlookInspectors_NewInspector);
OutlookApplication.ItemSend +=new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemSendEventHandler(OutlookApplication_ItemSend);
7.Add the event handler function OutlookInspectors_NewInspector:
void OutlookInspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
{
OutlookInspector = (Outlook.Inspector)Inspector;
if (Inspector.CurrentItem is Outlook.MailItem)
{
OutlookMailItem = (Outlook.MailItem)Inspector.CurrentItem;
}
}
8.Add the event handler function OutlookApplication_ItemSend:
void OutlookApplication_ItemSend(object Item, ref bool Cancel)
{
string strchkTo = OutlookMailItem.To;
string strchk = "hello Welcome to c#";
MessageBox.Show(strchk + "
" + strchkTo);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…