Problem with ItemAdd()

Problem with ItemAdd()

Post by Um9iaW » Sun, 20 Feb 2005 00:53:05


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");
}
 
 
 

Problem with ItemAdd()

Post by Helmut Obe » Wed, 23 Feb 2005 04:17:00

i 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' />