Ken,
Here is a sample, I just deleted all the non-relevant code from my addin
namespace TODOTest
{
using System;
using Extensibility;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using stdole;
using System.Drawing;
using System.Text;
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
MyAddin1Setup
project,
// right click the project in the Solution Explorer, then choose
install.
//
//
// When using this template make sure to change the AssemblyInfo module
to use your own
// settings and change the addin GUID to a new unique GUID, as well
as
changing the
// ProgId for the addin.
//
// Also change the target folder for deployment in the Setup project.
//
//
// This template does not include a shim, recommended for any shared
managed code addin,
// a shim that can be modified for use with this template is included
with the shared
// addins available for download from the Office Web site.
#endregion
/// <summary>
/// The object for implementing an Add-in.
/// </summary>
/// <see also class='IDTExtensibility2' />
[GuidAttribute("162B9E04-2ADB-481D-867E-8D8A4FBAC54F"),
ProgId("TODOTest.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{
#region Module_level_declarations
public static string m_ProgID = "";
// start of COM objects
private object applicationObject = null;
public Outlook.Application m_Outlook = null;
public Outlook.NameSpace m_NameSpace = null;
private Office.COMAddIn addInInstance = null;
private Outlook.Folder m_Folder = null;
private Outlook.Items m_Items = null;
// end of COM objects
//Initialization flags
private bool m_Teardown = false;
private bool m_Init = false;
#endregion
/// <summary>
/// Implements the constructor for the Add-in object.
/// Place your initialization code within this method.
/// </summary>
public Connect()
{
}
#region Startup_Shutdown
/// <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)
{
Debug.WriteLine("Connect - OnConnection");
applicationObject = application;
try
{
addInInstance = (Office.COMAddIn)addInInst;
}
catch
{
addInInstance = null;
}
if (addInInstance != null)
{
//set module level reference to COMAddIn object
try
{
m_Outlook = (Outlook.Application)application;
m_NameSpace = m_Outlook.GetNamespace("MAPI");
try
{
//put ProgID in a module level variable
m_ProgID = addInInstance.ProgId;
//addInInstance.Object = Me
addInInstance.GetType().InvokeMember("Object",
BindingFlags.Public | BindingFlags.SetProperty, null, addInInst, new
object[]
{ this });
m_Teardown = false;
m_Folder =
(Outlook.Folder)m_NameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
m_Folder.BeforeItemMove += new
Microsoft.Office.Interop.Outlook.MAPIFolderEvents_12_BeforeItemMoveEventHandler(m_Folder_BeforeItemMove);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
TearDown();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
TearDown();
}
}
else TearDown();
}
/// <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)
{
Debug.WriteLine("Connect - OnDisconnection");
if (m_Teardown == false) TearDown();
}
/// <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)
{
Debug.WriteLine("Connect - OnAddInsUpdate");
}
/// <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)
{
Debug.WriteLine("Connect - OnStartupComplete");
}
/// <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)
{
Debug.WriteLine("Connect - OnBeginShutdown");
}
#endregion
#region utility_procedures
public void InitHandler()
{
Debug.WriteLine("Connect - InitHandler");
//set initialization flag
m_Init = true;
}
private void TearDown()
{
Debug.WriteLine("Connect - TearDown");
if (m_Teardown == false)
{
try
{
if (m_Folder != null)
{
m_Folder.BeforeItemMove -= new
Microsoft.Office.Interop.Outlook.MAPIFolderEvents_12_BeforeItemMoveEventHandler(m_Folder_BeforeItemMove);
m_Folder = null;
}
if (m_NameSpace != null)
{
m_NameSpace = null;
}
if (m_Outlook != null)
{
m_Outlook = null;
}
if (applicationObject != null)
{
applicationObject = null;
}
if (addInInstance != null)
{
addInInstance = null;
}
m_Teardown = true;
m_Init = false;
GC.Collect();
GC.WaitForPendingFinalizers();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
#endregion
void m_Folder_BeforeItemMove(object Item,
Microsoft.Office.Interop.Outlook.MAPIFolder MoveTo, ref bool Cancel)
{
Debug.WriteLine("Connect - m_Folder_BeforeItemMove");
Outlook.AppointmentItem _apItem =
(Outlook.AppointmentItem)Item;
Debug.WriteLine(_apItem.Subject);
_apItem = null;
}
}
}
//END====================================
Ken Slovak - said:
I'd have to check out that situation, I haven't seen the problem myself.
I'll try to get a chance today to fix up some code to handle that event
and
see what happens with it. Or, if your code is short enough and you can
post
it here I can sample that as part of a test.