This is a sample c# AddIn, hooking the SentFolder Items Add Event, and saves Information from outgoing Emails to an XML file. (Saving sent email Information to XML c# )
CODE:
namespace TestAddIn
{
using System;
using Off = Microsoft.Office.Core;
using Ol = Microsoft.Office.Interop.Outlook ;
using Extensibility; using System.Runtime.InteropServices;
using System.Data;
using System.Xml;
using System.IO;
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
///
/// The object for implementing an Add-in.
///
///
[GuidAttribute("CFC4FD8C-6769-46C2-8B32-F3ACA3A14CF1"),
ProgId("TestAddIn.Connect")]
public class Connect : Object,
Extensibility.IDTExtensibility2
{
// The Instance Object private object myInstance;
// The Application Object private Ol.Application myApplication;
// The Sent Folder private Ol.MAPIFolder mySentFolder;
// My DataSet private DataSet myDataSet;
// My DataTable private DataTable myDataTable;
///
/// 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)
{
try
{ myApplication = (Ol.Application) application;
myInstance = addInInst;
LoadXMLData();
mySentFolder = myApplication.Session.GetDefaultFolder (Ol.OlDefaultFolders.olFolderSentMail );
mySentFolder.Items.ItemAdd +=new Microsoft.Office.Interop.Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);
}
catch (System.Exception ex)
{
Debug.WriteLine(ex);
}
}
///
/// 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)
{
myInstance = null;
if (mySentFolder != null)
{
// DeRegister Event mySentFolder.Items.ItemAdd -=new Microsoft.Office.Interop.Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);
Marshal.ReleaseComObject(myApplication);
}
if (myApplication != null)
{
Marshal.ReleaseComObject(myApplication);
}
GC.Collect ();
}
///
/// 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)
{
}
///
/// 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)
{
}
///
/// Loads the Data from XML File
///
private void LoadXMLData()
{
// Create a new DataSet myDataSet = new DataSet ("SentData");
// Check, if File exists if (File.Exists (@"C:\mydata.xml"))
{
// Yes, read the XML-Data into Dataset myDataSet.ReadXml (@"C:\mydata.xml");
// Get the DataTable
if (myDataSet.Tables.Count > 0)
{
myDataTable = myDataSet.Tables [0];
}
}
if (myDataTable == null)
{
// Create a new DataTable myDataTable = new DataTable("DataTable");
// Add Columns to DataTable (would be nicer with a SchemaFile) myDataTable.Columns.Add ("EntryID",Type.GetType ("System.String")); myDataTable.Columns.Add ("Subject",Type.GetType("System.String")); myDataTable.Columns.Add ("SentOn",Type.GetType("System.DateTime"));
myDataSet.Tables.Add (myDataTable);
SaveXMLData();
}
}
///
/// Saves the Data to XML
///
private void SaveXMLData()
{
// Accept Changes myDataSet.AcceptChanges ();
// Save the XML Data myDataSet.WriteXml (@"C:\mydata.xml");
}
///
/// EventHandler for Items Add Event of Sent Folder
///
/// The Outlook Item
private void Items_ItemAdd(object Item)
{
Ol.MailItem myMail = null; try
{
// Check Item, could be a MeetingRequest also if (Item is Ol.MailItem)
{
// Cast Object to MailItem myMail = (Ol.MailItem) Item;
// Create a new DataRow DataRow newRow = myDataTable.NewRow ();
// Fill Data into Row newRow["EntryID"] = myMail.EntryID; newRow["Subject"] = myMail.Subject ; newRow["SentOn"] = myMail.SentOn ;
// Add Row to DataTable myDataTable.Rows.Add (newRow);
myDataTable.AcceptChanges ();
// Save new Data SaveXMLData();
}
}
catch (System.Exception ex)
{
Debug.WriteLine(ex.Message );
}
finally
{ // Release the Reference to MailObject if (myMail != null) { Marshal.ReleaseComObject (myMail);
}
}
}
}
}
1 comments:
This code is ripped from
http://www.outlookcode.com/codedetail.aspx?id=767
:-(
Post a Comment