More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

share code 25-Sep-2018 08:06

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: Add Button to Application and Mail Item in c#
Description: Adds button at Application and Mail Item level, based on tutorial on http://support.microsoft.com/?kbid=30290.
Date: 15-Jun-2004  04:31
Code level: intermediate
Code area: Outlook and .NET
Posted by: Peter Mann
This message is displayed as VB.NET
 
namespace Office2000Addin
{
using System;
using Microsoft.Office.Core;
using Extensibility;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Threading;
#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("7EFF9BA9-5ACA-44C8-8185-3BE243DF92AF"), ProgId("Office2000Addin.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{
	/// <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)
	{
		applicationObject = application;
		addInInstance = addInInst;

		if(connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup)
		{
			OnStartupComplete(ref custom);
		}
	}

	/// <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)
	{
		if(disconnectMode != Extensibility.ext_DisconnectMode.ext_dm_HostShutdown)
		{
			OnBeginShutdown(ref custom);
		}
		applicationObject = null;
	}

	/// <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)
	{
		LoadMailItemBar();
		CommandBars oCommandBars;
		CommandBar oStandardBar;

		try
		{
			oCommandBars = (CommandBars)applicationObject.GetType().InvokeMember("CommandBars", BindingFlags.GetProperty , null, applicationObject ,null);

		}
		catch(Exception)
		{
			// Outlook has the CommandBars collection on the Explorer object.
			object oActiveExplorer;
			oActiveExplorer= applicationObject.GetType().InvokeMember("ActiveExplorer",BindingFlags.GetProperty,null,applicationObject,null);
			oCommandBars= (CommandBars)oActiveExplorer.GetType().InvokeMember("CommandBars",BindingFlags.GetProperty,null,oActiveExplorer,null);
		}

		// Set up a custom button on the "Standard" commandbar.
		try
		{
			oStandardBar = oCommandBars["Standard"]; 
				
		}
		catch(Exception)
		{
			// Access names its main toolbar Database.
			oStandardBar = oCommandBars["Database"];      
		}

		// In case the button was not deleted, use the exiting one.
		try
		{
			MyButton = (CommandBarButton)oStandardBar.Controls["MyButton"]; 
		}
		catch(Exception)
		{
			object omissing = System.Reflection.Missing.Value ;


			MyButton = (CommandBarButton)oStandardBar.Controls.Add(MsoControlType.msoControlButton,
				System.Reflection.Missing.Value,
				System.Reflection.Missing.Value,
				System.Reflection.Missing.Value,
				false);
			//
			MyButton.Caption = "MyButton";
			MyButton.Style = MsoButtonStyle.msoButtonIconAndCaption;
			MyButton.FaceId = 3;
			MyButton.TooltipText = "MyButton";

		}

		// The following items are optional, but recommended. 
		//The Tag property lets you quickly find the control 
		//and helps MSO keep track of it when more than
		//one application window is visible. The property is required
		//by some Office applications and should be provided.
		MyButton.Tag = "MyButton";

		// The OnAction property is optional but recommended. 
		//It should be set to the ProgID of the add-in, so that if
		//the add-in is not loaded when a user presses the button,
		//MSO loads the add-in automatically and then raises
		//the Click event for the add-in to handle. 
		MyButton.OnAction = "!<Office2000Addin.Connect>";

		MyButton.Visible = true;
		MyButton.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.MyButton_Click);

		object oName = applicationObject.GetType().InvokeMember("Name",BindingFlags.GetProperty,null,applicationObject,null);
			

		oStandardBar = null;
		oCommandBars = null;


	}

	/// <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)
	{
		object omissing = System.Reflection.Missing.Value ;

		MyButton.Delete(omissing);
		MyButton = null;
		MyButton2.Delete(omissing);
		MyButton2 = null;

	}



	private void MyButton_Click(CommandBarButton cmdBarbutton,ref bool cancel) 
	{
// following lines would be where you call the application. In this case, "Form1"
		Form1 f = new Form1();
		f.Show();
	}

	private void MyButton2_Click(CommandBarButton cmdBarbutton,ref bool cancel) 
	{
// following lines would be where you call the application. In this case, "Form1"
		Form1 f = new Form1();
		f.Show();
	}
	
	private void Inspectors_NewInspector(Outlook.Inspector i)
	{
		Office.CommandBars oCommandBars;
		Office.CommandBar oStandardBar;
		oCommandBars =i.CommandBars;
		oStandardBar = oCommandBars["Standard"];

		MyButton2 = (CommandBarButton)oStandardBar.FindControl(System.Reflection.Missing.Value, 
			System.Reflection.Missing.Value,
			"MyButton2",System.Reflection.Missing.Value,
			System.Reflection.Missing.Value);
		if(MyButton2 == null)
		{
			MyButton2 = (CommandBarButton)oStandardBar.Controls.Add(MsoControlType.msoControlButton,
				System.Reflection.Missing.Value,
				System.Reflection.Missing.Value,
				System.Reflection.Missing.Value,
				false);
			MyButton2.Caption = "MyButton2";
			MyButton2.Style = MsoButtonStyle.msoButtonIconAndCaption;
			MyButton2.FaceId = 3;
			MyButton2.TooltipText = "MyButton2";
			MyButton2.BeginGroup = false;
			MyButton2.Tag = "MyButton2";
		}
		MyButton2.OnAction = "<Office2000Addin.Connect>";
		MyButton2.Visible = true;
		MyButton2.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.MyButton2_Click);
	}


	private object applicationObject;
	private object addInInstance;
	private CommandBarButton MyButton;
	private CommandBarButton MyButton2;
	public Outlook._Application outApp;



	public void LoadMailItemBar()
	{
		if (applicationObject.GetType().InvokeMember("Name",BindingFlags.GetProperty,null,applicationObject,null).ToString()=="Outlook")
		{
			object outl = applicationObject;
			outApp = outl as Outlook._Application;
			outApp.Inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(this.Inspectors_NewInspector);
		}
	}
}
}
All 4comments
Page [ 1  
  16-Jun-2004  05:37   
Sorry - the article is http://support.microsoft.com/?kbid=302901.
  13-Jul-2004  02:21   
Hi There!
I have a problem with the solution described here.
Problem Description:
The first time I started a new Mail-Item everything works fine, I click on the "mybutton2" and the click event fires one time and "MyButton2_Click" runs one time.
But if I start a new Mail-Item a second time, and then click on the "mybutton2" the click-event fires two times and the method "MyButton2_Click" runs two times.
Can somebody help me with this?
  03-Nov-2004  13:23   

private void Inspectors_NewInspector(Outlook.Inspector i)
    {
        Office.CommandBars oCommandBars;
        Office.CommandBar oStandardBar;
        oCommandBars =i.CommandBars;
        oStandardBar = oCommandBars["Standard"];

....................


The function is getting hit properly when I do Create New Message, but the call to
oStandardBar = oCommandBars["Standard"]; fails.

Any clue. I have the standard toolbar displayed in New Messagea mong other toolbars like Email
  04-Aug-2005  03:40   
which reference have you done?I got a mistake about reference like this:
C:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\Office2000Addin\Connect.cs(251): The type or namespace name 'Outlook' could not be found (are you missing a using directive or an assembly reference?)
 
Page [ 1