K
Kevin
I follow the example on this article. http://support.microsoft.com/default.aspx?scid=kb;EN-US;302901 and wrote an addin for word 2003. the COM addin load ok but the commandbar buttons that I added doesn't work properly, I add the click event for each of the button that I add, e.g I added a Text Popup commandbar item and provided a click handler for it. but when it's clicked, all the handlers for all buttons are invoked in this addin, what's going on? I event use different handler name for each of the handler. the problem still persist.
here's the code from the connect.cs
namespace NRCANAcronyms
{
using System;
using Microsoft.Office.Core;
using Extensibility;
using System.Runtime.InteropServices;
using System.Reflection;
#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("C5CE6F09-378D-420B-A808-94370868E118"), ProgId("NRCANAcronyms.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{
private CommandBarButton MyButton;
private CommandBarButton DefPopup;
private CommandBarButton HyperPopup;
/// <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)
{
CommandBars oCommandBars= null;
CommandBar oStandardBar=null;
CommandBar oTextPopup=null;
CommandBar oHyperPopup=null;
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"];
oTextPopup = oCommandBars["Text"];
oHyperPopup = oCommandBars["Hyperlink Context Menu"];
}
catch(Exception)
{
// Access names its main toolbar Database.
//oStandardBar = oCommandBars["Database"];
System.Windows.Forms.MessageBox.Show("Error retrieving commandbar object");
}
// In case the button was not deleted, use the exiting one.
try
{
MyButton = (CommandBarButton)oStandardBar.Controls["Definition"];
}
catch(Exception)
{
object omissing = System.Reflection.Missing.Value ;
MyButton = (CommandBarButton) oStandardBar.Controls.Add(1, omissing , omissing , omissing , omissing);
MyButton.Caption = "Definition";
MyButton.FaceId = 1561;
//MyButton.Style = MsoButtonStyle.msoButtonCaption;
MyButton.Style = MsoButtonStyle.msoButtonIcon;
}
// 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 = "Look up definition";
// 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 = "!<NRCANAcronyms.Connect>";
MyButton.Visible = true;
MyButton.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.MyButton_Click);
// In case the button was not deleted, use the exiting one.
try
{
DefPopup = (CommandBarButton)oTextPopup.Controls["Definition"];
}
catch(Exception)
{
object omissing = System.Reflection.Missing.Value ;
DefPopup = (CommandBarButton) oTextPopup.Controls.Add(1, omissing , omissing , omissing , omissing);
DefPopup.Caption = "Definition";
DefPopup.FaceId = 1561;
DefPopup.Style = MsoButtonStyle.msoButtonIconAndCaption;
}
// 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.
DefPopup.Tag = "Look up definition";
// 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.
//DefPopup.OnAction = "!<NRCANAcronyms.Connect>";
DefPopup.Visible = true;
DefPopup.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.TextPopup_Click);
try
{
HyperPopup = (CommandBarButton)oHyperPopup.Controls["Definition"];
}
catch(Exception)
{
object omissing = System.Reflection.Missing.Value ;
HyperPopup = (CommandBarButton) oHyperPopup.Controls.Add(1, omissing , omissing , omissing , omissing);
HyperPopup.Caption = "Definition";
HyperPopup.FaceId = 1561;
HyperPopup.Style = MsoButtonStyle.msoButtonIconAndCaption;
}
// 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.
HyperPopup.Tag = "Look up definition";
// 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.
HyperPopup.OnAction = "!<NRCANAcronyms.Connect>";
HyperPopup.Visible = true;
HyperPopup.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.HyperPopup_Click);
object oName = applicationObject.GetType().InvokeMember("Name",BindingFlags.GetProperty,null,applicationObject,null);
// Display a simple message to show which application you started in.
System.Windows.Forms.MessageBox.Show("This Addin is loaded by " + oName.ToString() , "NRCAN Acronyms");
oStandardBar = null;
oCommandBars = null;
oTextPopup = null;
oHyperPopup = 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 ;
System.Windows.Forms.MessageBox.Show("MyCOMAddin Add-in is unloading.");
MyButton.Delete(omissing);
MyButton = null;
DefPopup.Delete(omissing);
DefPopup = null;
HyperPopup.Delete(omissing);
HyperPopup = null;
}
private object applicationObject;
private object addInInstance;
private void MyButton_Click(CommandBarButton cmdBarbutton,ref bool cancel)
{
System.Windows.Forms.MessageBox.Show("MyButton was Clicked","NRCAN Acronyms");
}
private void TextPopup_Click(CommandBarButton cmdBarbutton,ref bool cancel)
{
System.Windows.Forms.MessageBox.Show("Popup was Clicked","NRCAN Acronyms");
}
private void HyperPopup_Click(CommandBarButton cmdBarbutton,ref bool cancel)
{
System.Windows.Forms.MessageBox.Show("HyperPopup was Clicked","NRCAN Acronyms");
}
}
}
here's the code from the connect.cs
namespace NRCANAcronyms
{
using System;
using Microsoft.Office.Core;
using Extensibility;
using System.Runtime.InteropServices;
using System.Reflection;
#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("C5CE6F09-378D-420B-A808-94370868E118"), ProgId("NRCANAcronyms.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{
private CommandBarButton MyButton;
private CommandBarButton DefPopup;
private CommandBarButton HyperPopup;
/// <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)
{
CommandBars oCommandBars= null;
CommandBar oStandardBar=null;
CommandBar oTextPopup=null;
CommandBar oHyperPopup=null;
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"];
oTextPopup = oCommandBars["Text"];
oHyperPopup = oCommandBars["Hyperlink Context Menu"];
}
catch(Exception)
{
// Access names its main toolbar Database.
//oStandardBar = oCommandBars["Database"];
System.Windows.Forms.MessageBox.Show("Error retrieving commandbar object");
}
// In case the button was not deleted, use the exiting one.
try
{
MyButton = (CommandBarButton)oStandardBar.Controls["Definition"];
}
catch(Exception)
{
object omissing = System.Reflection.Missing.Value ;
MyButton = (CommandBarButton) oStandardBar.Controls.Add(1, omissing , omissing , omissing , omissing);
MyButton.Caption = "Definition";
MyButton.FaceId = 1561;
//MyButton.Style = MsoButtonStyle.msoButtonCaption;
MyButton.Style = MsoButtonStyle.msoButtonIcon;
}
// 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 = "Look up definition";
// 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 = "!<NRCANAcronyms.Connect>";
MyButton.Visible = true;
MyButton.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.MyButton_Click);
// In case the button was not deleted, use the exiting one.
try
{
DefPopup = (CommandBarButton)oTextPopup.Controls["Definition"];
}
catch(Exception)
{
object omissing = System.Reflection.Missing.Value ;
DefPopup = (CommandBarButton) oTextPopup.Controls.Add(1, omissing , omissing , omissing , omissing);
DefPopup.Caption = "Definition";
DefPopup.FaceId = 1561;
DefPopup.Style = MsoButtonStyle.msoButtonIconAndCaption;
}
// 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.
DefPopup.Tag = "Look up definition";
// 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.
//DefPopup.OnAction = "!<NRCANAcronyms.Connect>";
DefPopup.Visible = true;
DefPopup.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.TextPopup_Click);
try
{
HyperPopup = (CommandBarButton)oHyperPopup.Controls["Definition"];
}
catch(Exception)
{
object omissing = System.Reflection.Missing.Value ;
HyperPopup = (CommandBarButton) oHyperPopup.Controls.Add(1, omissing , omissing , omissing , omissing);
HyperPopup.Caption = "Definition";
HyperPopup.FaceId = 1561;
HyperPopup.Style = MsoButtonStyle.msoButtonIconAndCaption;
}
// 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.
HyperPopup.Tag = "Look up definition";
// 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.
HyperPopup.OnAction = "!<NRCANAcronyms.Connect>";
HyperPopup.Visible = true;
HyperPopup.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.HyperPopup_Click);
object oName = applicationObject.GetType().InvokeMember("Name",BindingFlags.GetProperty,null,applicationObject,null);
// Display a simple message to show which application you started in.
System.Windows.Forms.MessageBox.Show("This Addin is loaded by " + oName.ToString() , "NRCAN Acronyms");
oStandardBar = null;
oCommandBars = null;
oTextPopup = null;
oHyperPopup = 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 ;
System.Windows.Forms.MessageBox.Show("MyCOMAddin Add-in is unloading.");
MyButton.Delete(omissing);
MyButton = null;
DefPopup.Delete(omissing);
DefPopup = null;
HyperPopup.Delete(omissing);
HyperPopup = null;
}
private object applicationObject;
private object addInInstance;
private void MyButton_Click(CommandBarButton cmdBarbutton,ref bool cancel)
{
System.Windows.Forms.MessageBox.Show("MyButton was Clicked","NRCAN Acronyms");
}
private void TextPopup_Click(CommandBarButton cmdBarbutton,ref bool cancel)
{
System.Windows.Forms.MessageBox.Show("Popup was Clicked","NRCAN Acronyms");
}
private void HyperPopup_Click(CommandBarButton cmdBarbutton,ref bool cancel)
{
System.Windows.Forms.MessageBox.Show("HyperPopup was Clicked","NRCAN Acronyms");
}
}
}