namespace AyaNovaOL { using System; using Extensibility; using System.Runtime.InteropServices; using Outlook=Microsoft.Office.Interop.Outlook; using Microsoft.Office.Core; using System.Windows.Forms; using System.Reflection; using System.Collections.Generic; #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 AyaNovaOLSetup project, // right click the project in the Solution Explorer, then choose install. #endregion /// /// The object for implementing an Add-in. /// /// [GuidAttribute("E40A95F9-61D2-4B47-AF80-54BCC5AC9D7F"), ProgId("AyaNovaOL.Connect")] public class Connect : Object, Extensibility.IDTExtensibility2 { // Our UI will consist of a single CommandBarButton // private CommandBarButton btnAyaNova; private Outlook.Application _OutlookApplication; private object _addInInstance; // Wrapper stuff Outlook.Inspectors _Inspectors; Outlook.Explorers _Explorers; // This dictionary holds our Wrapped Inspectors, Explorers, Folders, Items, Whatever Dictionary _WrappedObjects; /// /// Implements the constructor for the Add-in object. /// Place your initialization code within this method. /// public Connect() { } /// /// Implements the OnConnection method of the IDTExtensibility2 interface. /// Receives notification that the Add-in is being loaded. /// /// /// Root object of the host application. /// /// /// Describes how the Add-in is being loaded. /// /// /// Object representing this Add-in. /// /// public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom) { Util.db("OLI -> OnConnection()"); //MessageBox.Show("OnConnection"); _OutlookApplication = application as Outlook.Application; _addInInstance = addInInst; if (connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup) { OnStartupComplete(ref custom); } } /// /// Implements the OnDisconnection method of the IDTExtensibility2 interface. /// Receives notification that the Add-in is being unloaded. /// /// /// Describes how the Add-in is being unloaded. /// /// /// Array of parameters that are host application specific. /// /// public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom) { //MessageBox.Show("OnDisconnection"); if (disconnectMode != Extensibility.ext_DisconnectMode.ext_dm_HostShutdown) { OnBeginShutdown(ref custom); } //MessageBox.Show("OnDisconnection completed"); } /// /// Implements the OnAddInsUpdate method of the IDTExtensibility2 interface. /// Receives notification that the collection of Add-ins has changed. /// /// /// Array of parameters that are host application specific. /// /// public void OnAddInsUpdate(ref System.Array custom) { } /// /// Implements the OnStartupComplete method of the IDTExtensibility2 interface. /// Receives notification that the host application has completed loading. /// /// /// Array of parameters that are host application specific. /// /// public void OnStartupComplete(ref System.Array custom) { _WrappedObjects = new Dictionary(); //// Inspectors stuff _Inspectors = _OutlookApplication.Inspectors; _Inspectors.NewInspector += new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(_Inspectors_NewInspector); // Are there any open Inspector after Startup ? for (int i = _Inspectors.Count; i >= 1; i--) { // Wrap the Inspector and do some usefull with it WrapInspector(_Inspectors[i]); } //// Explorer stuff _Explorers = _OutlookApplication.Explorers; _Explorers.NewExplorer += new Microsoft.Office.Interop.Outlook.ExplorersEvents_NewExplorerEventHandler(_Explorers_NewExplorer); // Are there any open Explorers after Startup ? for (int i = _Explorers.Count; i >= 1; i--) { // Wrap the Explorer and do some usefull with it WrapExplorer(_Explorers[i]); } } /// /// Implements the OnBeginShutdown method of the IDTExtensibility2 interface. /// Receives notification that the host application is being unloaded. /// /// /// Array of parameters that are host application specific. /// /// public void OnBeginShutdown(ref System.Array custom) { //MessageBox.Show("Cleanup starting in OnBeginShutdown"); CleanUp(); //MessageBox.Show("Cleanup completed in OnBeginShutdown"); } #region Wrapper specific void CleanUp() { if (_OutlookApplication != null) { _WrappedObjects.Clear(); _Inspectors.NewInspector -= new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(_Inspectors_NewInspector); _Inspectors = null; _Explorers.NewExplorer -= new Outlook.ExplorersEvents_NewExplorerEventHandler(_Explorers_NewExplorer); _Explorers = null; _addInInstance = null; _OutlookApplication = null; GC.Collect(); GC.WaitForPendingFinalizers(); } } void _Explorers_NewExplorer(Microsoft.Office.Interop.Outlook.Explorer Explorer) { WrapExplorer(Explorer); } void WrapExplorer(Microsoft.Office.Interop.Outlook.Explorer Explorer) { //Util.d("WrapExplorer:" + Explorer.Caption); ExplorerWrapper wrappedExplorer = new ExplorerWrapper(Explorer); wrappedExplorer.Closed += new WrapperClosedDelegate(wrappedObject_Closed); _WrappedObjects[wrappedExplorer.Id] = wrappedExplorer; } void _Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector) { if (Inspector.CurrentItem is Outlook.MailItem) { InspectorWrapper wrappedInspector = new InspectorWrapper(Inspector); wrappedInspector.Closed += new WrapperClosedDelegate(wrappedObject_Closed); _WrappedObjects[wrappedInspector.Id] = wrappedInspector; } } /// /// The Inspector is "wrapped" and used in the application. /// /// The new Inspector instance void WrapInspector(Outlook.Inspector inspector) { if (inspector.CurrentItem is Outlook.MailItem) { InspectorWrapper wrappedInspector = new InspectorWrapper(inspector); wrappedInspector.Closed += new WrapperClosedDelegate(wrappedObject_Closed); _WrappedObjects[wrappedInspector.Id] = wrappedInspector; } } void wrappedObject_Closed(Guid id) { _WrappedObjects.Remove(id); } #endregion } }