Problem with ItemAdd()

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am doing a Add-In for OUTLOOK by C Sharp, but i am getting trouble with a
quite weird problem.
actually, i wish to save the mail details when one mail sent.. but when i
make use of the save methods, which just execute once. Therefore, i use
messageBox to test it. It is quite amazing that sometimes it just execute
twice(Testing CASE1, see following code), sometimes it run three
times(Testing CASE2). However, if i just use messageBox, do not make use of
any other functions and methods, it's right..
I do not where is wrong??? and how to solve it???? I really need your help,
due to this is my final year project, which is so crucial for me!!! MANY
THANKS!!!

I really appreciate any help if anyone give to me....
ROBIN

the following are my codes:

public void OnStartupComplete(ref System.Array custom)
{
sentFolder = outlookNS.GetDefaultFolder(OlDefaultFolders.olFolderSentMail);
sentFolder.Items.ItemAdd +=new
ItemsEvents_ItemAddEventHandler(sentFolder_ItemAdd);
}

private void sentFolder_ItemAdd(object Ctrl)
{
MessageBox.Show("Sent Folder Testing1");
Microsoft.Office.Interop.Outlook.MailItem mailItem =
(Microsoft.Office.Interop.Outlook.MailItem) Ctrl;
MessageBox.Show("Sent Folder Testing2");

// Testing CASE1: which just execute twice to disply the testing messages,
in the third time, there is nothing to show
/*
mail_table.Add(mailItem, mailItem);
IDictionaryEnumerator enumerator = mail_table.GetEnumerator();

while(enumerator.MoveNext())
{
Microsoft.Office.Interop.Outlook.MailItem item =
(Microsoft.Office.Interop.Outlook.MailItem) enumerator.Key;
MessageBox.Show(item.SentOn.ToString()+ item.Recipients.Count.ToString() +
item.Subject);
}
*/


//Testing CASE2: the following execute three times to disply the testing
message, there is nothing to show in the fourth time.
//MessageBox.Show(mailItem.SentOn.ToString() + mailItem.Subject);
//MessageBox.Show("Sent Folder Testing3");
}
 
Hi Robin,

i already answerd you at www.outlookcode.com
just for the newsgroup, here is my sample code to do it.


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

/// <summary>
/// The object for implementing an Add-in.
/// </summary>
/// <seealso class='IDTExtensibility2' />
[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;

/// <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
{
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.Message);
}
}

/// <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)
{
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 ();
}

/// <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)
{
}

/// <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)
{
}

/// <summary>
/// Loads the Data from XML File
/// </summary>
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();

}

}

/// <summary>
/// Saves the Data to XML
/// </summary>
private void SaveXMLData()
{
// Accept Changes
myDataSet.AcceptChanges ();

// Save the XML Data
myDataSet.WriteXml (@"C:\mydata.xml");
}


/// <summary>
/// EventHandler for Items Add Event of Sent Folder
/// </summary>
/// <param name="Item">The Outlook Item</param>
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); }
}
}
}
}

Greets, Helmut Obertanner
 
Back
Top