More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

share code 23-Mar-2017 00:58

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

Code level: intermediate    Code area: Outlook and .NET Printer Friendly Version
Title: Hooking SyncObjects with C# AddIn
Description: Handle Sync start, end, error events in a C# AddIn
Date: 25-Feb-2005  07:36
Code level: intermediate
Code area: Outlook and .NET
Posted by: Helmut Obertanner
This message is displayed as VB.NET
 namespace XConnect
{
	using System;
	using System.Diagnostics ;
	using Microsoft.Office.Core;
	using Extensibility;
	using System.Runtime.InteropServices;
	using Ol = Microsoft.Office.Interop.Outlook ;  
	using MSO = Microsoft.Office.Core;  
	using System.Windows.Forms; 
	

	#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("9B73D13F-0476-4E16-86EF-A3A6B54EFFA8"), ProgId("XConnect.Connect")]
	public class AddIn : Object, Extensibility.IDTExtensibility2
	{

		private Ol.Application myApplicationObject;
		private object myAddInInstance;

		/// <summary>
		/// The SyncObject
		/// </summary>
		private Ol.SyncObjectClass mySyncObject = null; 


		/// <summary>
		///		Implements the constructor for the Add-in object.
		///		Place your initialization code within this method.
		/// </summary>
		public AddIn()
		{
		}

		/// <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)
		{
			myApplicationObject = (Ol.Application) application;
			myAddInInstance = addInInst;

		}

		/// <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)
		{
			mySyncObject.SyncEnd -= new Microsoft.Office.Interop.Outlook.SyncObjectEvents_SyncEndEventHandler(mySyncObject_SyncEnd);
			mySyncObject.OnError -= new Microsoft.Office.Interop.Outlook.SyncObjectEvents_OnErrorEventHandler(mySyncObject_OnError);
			mySyncObject.SyncStart -=new Microsoft.Office.Interop.Outlook.SyncObjectEvents_SyncStartEventHandler(mySyncObject_SyncStart);

			Marshal.ReleaseComObject (mySyncObject);
			Marshal.ReleaseComObject (myApplicationObject);
		}

		/// <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)
		{
			mySyncObject = (Ol.SyncObjectClass) myApplicationObject.Session.SyncObjects[1]; 

            mySyncObject.SyncEnd +=new Microsoft.Office.Interop.Outlook.SyncObjectEvents_SyncEndEventHandler(mySyncObject_SyncEnd);
			mySyncObject.OnError+=new Microsoft.Office.Interop.Outlook.SyncObjectEvents_OnErrorEventHandler(mySyncObject_OnError);
			mySyncObject.SyncStart +=new Microsoft.Office.Interop.Outlook.SyncObjectEvents_SyncStartEventHandler(mySyncObject_SyncStart);
		}

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

		private void mySyncObject_SyncStart()
		{
			MessageBox.Show ("SyncStart");
		}

		private void mySyncObject_SyncEnd()
		{
			MessageBox.Show  ("SyncEnd");
		}

		private void mySyncObject_OnError(int Code, string Description)
		{
			MessageBox.Show  ("SyncError: " + Description);
		}


	}
}
All 0comments