This Sample is created with Visual Studio .Net 2003 Shared AddIn Wizard.
There are 2 Files: Connect.cs and XMailItem.cs.
------------------------------------------------------------------------Connect.cs --------------------------------------------------------------------------
// ##################################################################################
//
// Sample for Wrapping Mail Inspectors and creates a CommandBar, MenuItems
// by Helmut Obertanner ( flash [at] x4u dot de)
//
// ##################################################################################
namespace XConnect
{
using System;
using System.Collections ;
using System.Diagnostics ;
using Microsoft.Office.Core;
using Extensibility;
using System.Runtime.InteropServices;
using System.Windows.Forms;
// Note: When AddIn was created with Visual Studio Wizard, remove reference to Office.dll and
// add reference to COM Office11 (Office 2003)
using Ol = Microsoft.Office.Interop.Outlook ;
using MSO = Microsoft.Office.Core;
#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
///
/// The object for implementing an Add-in.
///
///
[GuidAttribute("9B73D13F-0476-4E16-86EF-A3A6B54EFFA8"), ProgId("XConnect.Connect")]
public class AddIn : Object, Extensibility.IDTExtensibility2
{
///
/// The Outlook Application Object
///
private Ol.Application myApplicationObject;
///
/// My COM AddIn Instance
///
private object myAddInInstance;
///
/// The Inspectors Collection
///
private Ol.InspectorsClass myInspectors;
///
/// This Hashtable holds a reference to the active Inspectors
///
private Hashtable myActiveItems = null;
///
/// Implements the constructor for the Add-in object.
/// Place your initialization code within this method.
///
public AddIn()
{
}
///
/// 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)
{
// Get the initial Application object
myApplicationObject = (Ol.Application) application;
myAddInInstance = addInInst;
}
///
/// 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)
{
try
{
if (myInspectors != null)
{
// Unregister for NewInspector Event
myInspectors.NewInspector -=new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(myInspectors_NewInspector);
// Release Reference to COM Object
Marshal.ReleaseComObject (myInspectors);
}
myAddInInstance = null;
// Release Outlook COM Object
if (myApplicationObject != null) Marshal.ReleaseComObject (myApplicationObject);
// Perform a Garbage Collection
GC.Collect ();
}
catch (System.Exception ex)
{
MessageBox.Show (ex.Message);
}
}
///
/// 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)
{
try
{
// Create a new Hashtable Object
myActiveItems = new Hashtable (25);
// Get the Outlook Inspectors Collection
myInspectors = (Ol.InspectorsClass) myApplicationObject.Inspectors ;
// Register for the NewInspector event
myInspectors.NewInspector +=new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(myInspectors_NewInspector);
}
catch (System.Exception ex)
{
MessageBox.Show (ex.Message);
}
}
///
/// 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)
{
}
///
/// The eventhandler for the NewInspector event
/// Called when an Item is opened
///
///
private void myInspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
{
try
{
object Item = Inspector.CurrentItem;
// Check the ItemsType
if (Item is Ol.MailItem)
{
// Create a new Item wrapper Object
XMailItem myMail = new XMailItem (Item);
// Register for the Item Close event
myMail.Item_Closed +=new XEventHandler(myMail_Item_Closed);
// remember the Item in Collection
myActiveItems.Add (myMail.HashCode,myMail);
}
}
catch (System.Exception ex)
{
MessageBox.Show (ex.Message);
}
}
///
/// Eventhandler for the Item close event
///
///
///
private void myMail_Item_Closed(object sender, XEventArgs e)
{
// Remove Item from Collection
myActiveItems.Remove (e.HashCode );
}
}
}
-------------------------------------------- end of Connect.cs ------------------------------------------------------------------------------------------------------------
--------------------------------------------XMailItem.cs--------------------------------------------------------------------------------------------------------------------
// ##################################################################################
//
// Sample for Wrapping Mail Inspectors and creates a CommandBar, MenuItems
// by Helmut Obertanner ( flash [at] x4u dot de)
//
// ##################################################################################
using System;
using System.Diagnostics ;
using Microsoft.Office.Core;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Ol = Microsoft.Office.Interop.Outlook ;
using MSO = Microsoft.Office.Core;
namespace XConnect
{
///
/// EventArgs for my event
///
public class XEventArgs : EventArgs
{
private readonly int myHashCode = 0;
public int HashCode
{
get { return myHashCode; }
}
public XEventArgs(int hashCode)
{
myHashCode = hashCode;
}
}
///
/// A delegate for my events
///
public delegate void XEventHandler (object sender, XEventArgs e);
///
/// This is a wrapper for a MailItemObject
///
public class XMailItem
{
///
/// The "DATA"
///
private Ol.MailItem myMailItem;
///
/// the Inspector for the Item
///
private Ol.InspectorClass myInspector;
///
/// Event, raised when Item is closed
///
public event XEventHandler Item_Closed;
private int myID = 0;
private MSO.CommandBar myCommandBar = null;
private MSO.CommandBarComboBox myComboBox = null;
private MSO.CommandBarButton myButton = null;
private object myMissing = System.Reflection.Missing.Value ;
///
/// Returns the Hashcode for the Item
///
public int HashCode
{
get { return myMailItem.GetHashCode(); }
}
///
/// The Constructor for the XMailItem
///
/// The Outlook MailItem Object
public XMailItem(object Item)
{
// Remember the Object
myMailItem = (Ol.MailItem ) Item;
myID = myMailItem.GetHashCode ();
// Register for Item Open Event
myMailItem.Open +=new Microsoft.Office.Interop.Outlook.ItemEvents_10_OpenEventHandler(myMailItem_Open);
}
///
/// Eventhadler for a Mail Open event.
/// Remember this Item in ActiveItems
/// Create Button here and register for button click events
///
///
private void myMailItem_Open(ref bool Cancel)
{
// event isn't needed anymore
myMailItem.Open -=new Microsoft.Office.Interop.Outlook.ItemEvents_10_OpenEventHandler(myMailItem_Open);
// get the Inspector here
myInspector = (Ol.InspectorClass ) myMailItem.GetInspector ;
// register for the Inspector events
myInspector.InspectorEvents_Event_Close +=new Microsoft.Office.Interop.Outlook.InspectorEvents_CloseEventHandler(myInspector_InspectorEvents_Event_Close);
// Create the Menu
CreateMenu();
}
///
/// Eventhandler for the INspector close event
///
private void myInspector_InspectorEvents_Event_Close()
{
try
{
// Raise event, to remove us from active items collection
if (Item_Closed != null)
{
Item_Closed(this, new XEventArgs (myMailItem.GetHashCode() ));
}
// Cleanup resources
myInspector.InspectorEvents_Event_Close -=new Microsoft.Office.Interop.Outlook.InspectorEvents_CloseEventHandler(myInspector_InspectorEvents_Event_Close);
Marshal.ReleaseComObject (myInspector);
Marshal.ReleaseComObject (myMailItem);
}
catch (System.Exception ex)
{
MessageBox.Show (ex.Message);
}
}
///
/// Adds the Menu for the MailItem
///
private void CreateMenu()
{
try
{
if (myInspector == null) return ;
// Add our Own CommandBar to MailItem
myCommandBar = myInspector.CommandBars.Add ("XConnect", myMissing, myMissing, true);
// Add my ComboBox Control
myComboBox = (MSO.CommandBarComboBox) myCommandBar.Controls.Add (MSO.MsoControlType.msoControlComboBox , myMissing, myMissing, 1, 1);
myComboBox.Caption = "Company: ";
myComboBox.Width = 270;
myComboBox.Style = MSO.MsoComboStyle.msoComboLabel;
myComboBox.Tag = myID.ToString();
// Fill the ComboBox
FillComboFromDB();
myComboBox.Change +=new _CommandBarComboBoxEvents_ChangeEventHandler(myComboBox_Change);
// Add my button
myButton = (MSO.CommandBarButton) myCommandBar.Controls.Add (MSO.MsoControlType.msoControlButton, myMissing, myMissing, 2, 1);
myButton.Caption = "XMail";
myButton.Tag = myID.ToString();
myButton.Style = MSO.MsoButtonStyle.msoButtonIconAndCaption;
// Use one off 2 zilliards Office Icons....
myButton.FaceId = 24;
// Register for Click event
myButton.Click +=new _CommandBarButtonEvents_ClickEventHandler(myButton_Click);
// Make Menu Visible in TOP of Menus
myCommandBar.Visible = true;
myCommandBar.Enabled = true;
myCommandBar.Position = MSO.MsoBarPosition.msoBarTop;
}
catch (System.Exception ex)
{
MessageBox.Show (ex.Message);
}
}
///
/// This function Fills the ComboBox from DataBase
///
private void FillComboFromDB()
{
/// Simply a Jumpstart, not a complete solution
/// Solutions costs money ;-)
try
{
myComboBox.Clear ();
// Should come from DB
myComboBox.AddItem ("Foo",1);
myComboBox.AddItem ("Bar",2);
myComboBox.AddItem ("X4U",3);
myComboBox.AddItem ("Slipstick",4);
myComboBox.AddItem ("Turtleflock",5);
myComboBox.AddItem ("DATALOG",6);
}
catch (System.Exception ex)
{
MessageBox.Show (ex.Message);
}
}
///
/// Cleans up any resources for shutdown
///
private void CleanUp()
{
try
{
}
catch (System.Exception ex)
{
MessageBox.Show (ex.Message);
}
}
///
/// The ResetButton Click eventhandler
///
///
///
private void myButton_Click(CommandBarButton Ctrl, ref bool CancelDefault)
{
// Check, if we are in the right MailItem
if (Ctrl.Tag != myID.ToString ()) return;
myMailItem.Subject = "";
}
///
/// Event comes, when Combobox has changed
///
///
private void myComboBox_Change(CommandBarComboBox Ctrl)
{
// Check, if we are in the right MailItem
if (Ctrl.Tag != myID.ToString ()) return;
CalculateMailSubject();
}
///
/// Creates the new Mail subject
///
private void CalculateMailSubject()
{
try
{
string subject = "Learned from Company: " + myComboBox.Text + " @" + DateTime.Now.Hour + ":" + DateTime.Now.Minute ;
// assign new subject to MailItem
myMailItem.Subject = subject;
}
catch (System.Exception ex)
{
MessageBox.Show (ex.Message);
}
}
}
}
-----------------------------------------------------end of XMailItem.cs------------------------------------------------------------------------------
Free to use and modify to your requirements.
Greets, Helmut Obertanner
Monday, October 13, 2008
Outlook walkthrough by Helmut Obertanner
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment