Ken,
I am not sure what you mean by instantiate the addin. My code is an Outlook
addin and it obtains a handle to the Outlook application and handles the
Application.ItemSend event. Still this does not work.
I must be missing something
Your help is greatly appreciated.
Here is my code:
+++++++++++++++++++++++++++++++++++++++++
namespace Outlook_Events
{
using System;
//using System.Configuration;
using System.IO;
using Microsoft.Office.Core;
using Extensibility;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Outlook;
using System.Windows.Forms;
using System.Reflection;
using System.Diagnostics;
#region Read me for Add-in installation and setup information.
// When run, the Add-in wizard prepared the registry for the Add-in.
// At a later time, if the Add-in becomes unavailable for reasons such as:
// 1) You moved this project to a computer other than which is was
originally created on.
// 2) You chose 'Yes' when presented with a message asking if you wish to
remove the Add-in.
// 3) Registry corruption.
// you will need to re-register the Add-in by building the MyAddin21Setup
project
// by right clicking the project in the Solution Explorer, then choosing
install.
#endregion
/// <summary>
/// The object for implementing an Add-in.
/// </summary>
/// <seealso class='IDTExtensibility2' />
[GuidAttribute("1B6A385B-DF44-4DB7-9A36-1C30FA7FC512"),
ProgId("Outlook_Events.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{
/// <summary>
/// Implements the constructor for the Add-in object.
/// Place your initialization code within this method.
/// </summary>
public Connect()
{
}
/// <summary>
/// Implements the OnConnection method of the IDTExtensibility2
interface.
/// Receives notification that the Add-in is being loaded.
/// </summary>
/// <param term='application'>
/// Root object of the host application.
/// </param>
/// <param term='connectMode'>
/// Describes how the Add-in is being loaded.
/// </param>
/// <param term='addInInst'>
/// Object representing this Add-in.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnConnection(object application, Extensibility.ext_ConnectMode
connectMode, object addInInst, ref System.Array custom)
{
try
{
applicationObject =
(Microsoft.Office.Interop.Outlook.Application)application;
//++++++++++++++++++++++++++++++++++++++++++++++++++
applicationObject.ItemSend +=new
ApplicationEvents_11_ItemSendEventHandler(applicationObject_ItemSend);
//++++++++++++++++++++++++++++++++++++++++++++++++++
addInInstance = addInInst;
if(connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup)
{
OnStartupComplete(ref custom);
}
}
catch{}
}
/// <summary>
/// Implements the OnDisconnection method of the IDTExtensibility2
interface.
/// Receives notification that the Add-in is being unloaded.
/// </summary>
/// <param term='disconnectMode'>
/// Describes how the Add-in is being unloaded.
/// </param>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnDisconnection(Extensibility.ext_DisconnectMode
disconnectMode, ref System.Array custom)
{
if(disconnectMode != Extensibility.ext_DisconnectMode.ext_dm_HostShutdown)
{
OnBeginShutdown(ref custom);
}
applicationObject = null;
}
/// <summary>
/// Implements the OnAddInsUpdate method of the IDTExtensibility2
interface.
/// Receives notification that the collection of Add-ins has changed.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnAddInsUpdate(ref System.Array custom)
{
}
/// <summary>
/// Implements the OnStartupComplete method of the IDTExtensibility2
interface.
/// Receives notification that the host application has completed
loading.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnStartupComplete(ref System.Array custom)
{
//MessageBox.Show ("OnStartupComplete");
CommandBars oCommandBars;
CommandBar oStandardBar;
try
{
oCommandBars
= (CommandBars)applicationObject.GetType().InvokeMember("CommandBars",BindingFlags.GetProperty , null, applicationObject ,null);
}
catch(System.Exception)
{
object oActiveExplorer;
oActiveExplorer=
applicationObject.GetType().InvokeMember("ActiveExplorer",BindingFlags.GetProperty,null,applicationObject,null);
oCommandBars=
(CommandBars)oActiveExplorer.GetType().InvokeMember("CommandBars",BindingFlags.GetProperty,null,oActiveExplorer,null);
}
try
{
oStandardBar = oCommandBars["Standard"];
}
catch(System.Exception)
{
oStandardBar = oCommandBars["Database"];
}
try
{
MyButton=(CommandBarButton)oStandardBar.Controls["Reply to A&ll"];
}
catch(System.Exception)
{
}
try
{
MyButton.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.MyButton_Click);
oStandardBar = null;
oCommandBars = null;
colInspectors = (InspectorsClass) applicationObject.Inspectors;
colExplorers = (ExplorersClass) applicationObject.Explorers;
oExplorer = (ExplorerClass) applicationObject.ActiveExplorer();
colInspectors.NewInspector += new
InspectorsEvents_NewInspectorEventHandler(OnColInspectors_NewInspector);
oExplorer.SelectionChange += new
ExplorerEvents_10_SelectionChangeEventHandler(OnoExplorer_SelectionChange);
}
catch{}
}
private void MyButton_Click(CommandBarButton cmdBarbutton,ref bool cancel)
{
System.Windows.Forms.DialogResult retVal;
retVal = System.Windows.Forms.MessageBox.Show("Replying to all can cause
unnecessary email traffic – are you sure you want to reply to
everyone?","Warning!",System.Windows.Forms.MessageBoxButtons.YesNo,System.Windows.Forms.MessageBoxIcon.Warning);
if(retVal==System.Windows.Forms.DialogResult.Yes)
{
cancel=false;
}
else
{
cancel=true;
}
bReplyAllFlag = true;
}
/// <summary>
/// Implements the OnBeginShutdown method of the IDTExtensibility2
interface.
/// Receives notification that the host application is being unloaded.
/// </summary>
/// <param term='custom'>
/// Array of parameters that are host application specific.
/// </param>
/// <seealso class='IDTExtensibility2' />
public void OnBeginShutdown(ref System.Array custom)
{
try
{
Process[] oProcs = Process.GetProcessesByName("showform.ixos");
System.Collections.IEnumerator myEnumerator = oProcs.GetEnumerator();
while ( myEnumerator.MoveNext() )
{
((Process)myEnumerator.Current).Kill();
}
}
catch{}
}
public void
OnColInspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector
mInspector )
{
//MessageBox.Show("OnColInspectors_NewInspector");
try
{
if (mInspector.CurrentItem.GetType().Name=="MailItemClass")
{
oMailItem = (MailItemClass)mInspector.CurrentItem;
oMailItem.ItemEvents_Event_ReplyAll += new
ItemEvents_ReplyAllEventHandler(onMailItemReplyAll);
//oMailItem.ItemEvents_Event_Send += new
Microsoft.Office.Interop.Outlook.ItemEvents_SendEventHandler(onMailItemSend);
}
}
catch(System.Exception e)
{
//MessageBox.Show("OnColInspectors_NewInspector: " + e.Message);
}
}
public void onMailItemReplyAll(Object mResonse, ref bool Cancel)
{
try
{
if (!bReplyAllFlag)
{
System.Windows.Forms.DialogResult retVal;
retVal = System.Windows.Forms.MessageBox.Show("Replying to all can
cause unnecessary email traffic – are you sure you want to reply to
everyone?","Warning!",System.Windows.Forms.MessageBoxButtons.YesNo,System.Windows.Forms.MessageBoxIcon.Warning);
if(retVal==System.Windows.Forms.DialogResult.Yes)
{
Cancel=false;
}
else
{
Cancel=true;
}
}
}
catch{}
}
/*public void onMailItemSend( ref bool Cancel)
{
}*/
private void applicationObject_ItemSend(object Item, ref bool Cancel)
{
//MessageBox.Show( "applicationObject_ApplicationEvents_Event_ItemSend");
string ItemType;
System.Collections.IEnumerator myEnumerator=null;
try
{
ItemType = Item.GetType().Name;
switch (ItemType)
{
case "AppointmentItemClass":
myEnumerator =
((AppointmentItemClass)Item).Attachments.GetEnumerator();
break;
case "MeetingItemClass":
myEnumerator = ((MeetingItemClass)Item).Attachments.GetEnumerator();
break;
case "MailItemClass":
myEnumerator = ((MailItemClass)Item).Attachments.GetEnumerator();
break;
case "__ComObject":
oMailItem= (MailItemClass)oExplorer.Selection[1];
myEnumerator = oMailItem.Attachments.GetEnumerator();
break;
case "TaskItemClass":
myEnumerator = ((TaskItemClass)Item).Attachments.GetEnumerator();
break;
}
//AppointmentItem, MeetingItem, MailItem, or TaskItem
//System.Collections.IEnumerator myEnumerator =
oMailItem.Attachments.GetEnumerator();
/*(if (Item.GetType().Name!="MailItemClass")
{
return;
}*/
//System.Collections.IEnumerator myEnumerator =
((MailItemClass)Item).Attachments.GetEnumerator();
Attachment oAttachment;
string tmpPath= Environment.GetEnvironmentVariable("TEMP");
//MessageBox.Show ("tmpPath: " + tmpPath);
long totalSize = 0;
while ( myEnumerator.MoveNext() )
{
try
{
oAttachment= (Attachment)myEnumerator.Current;
oAttachment.SaveAsFile (tmpPath + @"\tmpAttachment.tmp");
FileInfo oFileInfo;
oFileInfo = new FileInfo(tmpPath + @"\tmpAttachment.tmp");
totalSize += oFileInfo.Length ;
oFileInfo.Delete();
}
catch(System.Exception e)
{
//MessageBox.Show(e.Message);
}
}
long maxAttSize;
#region Commented
//maxAttSize=
Convert.ToInt64(System.Configuration.ConfigurationSettings.AppSettings["maxAttachmentsSize"].ToString());
//(Inspector)applicationObject.ActiveWindow
//MessageBox.Show (applicationObject.ActiveInspector().Caption);
#endregion Commented
maxAttSize =512000;
if (totalSize > maxAttSize)
{
System.Windows.Forms.DialogResult retVal;
retVal = System.Windows.Forms.MessageBox.Show(new
WindowWrapper(GetForegroundWindow()) , "This message is over 500kb – if it’s
going to recipients who use dial up, it could inconvenience them. Are you
sure you want to send this
message?","Warning!",System.Windows.Forms.MessageBoxButtons.YesNo,System.Windows.Forms.MessageBoxIcon.Warning);
if(retVal==System.Windows.Forms.DialogResult.Yes)
{
Cancel=false;
}
else
{
Cancel=true;
}
}
}
catch (System.Exception e)
{
//MessageBox.Show("onMailItemSend: " + e.Message);
}
}
public void OnoExplorer_SelectionChange()
{
//MessageBox.Show( "OnoExplorer_SelectionChange");
#region Commented
/*try
{
if (!bReplyAllFlag)
{
oMailItem =
(MailItemClass)(applicationObject.ActiveExplorer().Selection[1]);
oMailItem.ItemEvents_Event_ReplyAll += new
ItemEvents_ReplyAllEventHandler(onMailItemReplyAll);
//oMailItem.ItemEvents_Event_Send += new
Microsoft.Office.Interop.Outlook.ItemEvents_SendEventHandler(onMailItemSend);
}
}
catch(System.Exception e)
{
//MessageBox.Show(e.Message);
}
*/
#endregion
bReplyAllFlag = false;
}
//private object applicationObject;
private Microsoft.Office.Interop.Outlook.Application applicationObject;
//private Microsoft.Office.Interop.Outlook.ApplicationClass oApp;
private object addInInstance;
//
private Microsoft.Office.Interop.Outlook.InspectorsClass colInspectors;
private Microsoft.Office.Interop.Outlook.ExplorersClass colExplorers;
//private Microsoft.Office.Interop.Outlook.InspectorClass oInspector;
private Microsoft.Office.Interop.Outlook.ExplorerClass oExplorer;
private Microsoft.Office.Interop.Outlook.MailItemClass oMailItem;
private CommandBarButton MyButton;
private bool bReplyAllFlag =false;
[ DllImport("user32.dll") ]
static extern IntPtr GetForegroundWindow();
}
public class WindowWrapper : System.Windows.Forms.IWin32Window
{
public WindowWrapper(IntPtr handle)
{
_hwnd = handle;
}
public IntPtr Handle
{
get { return _hwnd; }
}
private IntPtr _hwnd;
}
}
++++++++++++++++++++++++++++++++++++++++