A
Andrew Mercer
Hi,
I have created an addin for word that when loaded adds a button
to the Standard Tool Bar when MS Word is opened.
However each time I open MSWord a new instance of the button is added
to the toolbar.
Can anyone advise how I can prevent this.
A snippet of my code is attached, this is based on code from the following
Url:
http://www.rootsilver.com/2007/08/why-doesnt-my-custom-office-ad.html.
Thanks for any assistance
Regards Andrew
namespace MyReportSaver
{
using System;
using Extensibility;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Reflection;
using Microsoft.Office.Core;
/// <summary>
/// The object for implementing an Add-in.
/// </summary>
/// <seealso class='IDTExtensibility2' />
[GuidAttribute("8A7B4843-1B92-41D0-8A0C-FFE4E55E1E25"),
ProgId("MyReportSaver.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{
private CommandBarButton MyButton;
/// <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>
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>
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>
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>
public void OnStartupComplete(ref System.Array custom)
{
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["My
Custom Button"];
//MyButton.Visible = false;
}
catch (Exception)
{
object omissing = System.Reflection.Missing.Value;
MyButton =
(CommandBarButton)oStandardBar.Controls.Add(1, omissing,
omissing,
omissing, omissing);
MyButton.Caption = "My Custom Button";
}
MyButton.Tag = "My Custom Button";
MyButton.OnAction = "8A7B4843-1B92-41D0-8A0C-FFE4E55E1E25";
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);
// Display a simple message to show which application you
started in.
//System.Windows.Forms.MessageBox.Show("This Addin is loaded by
" + oName.ToString(), "MyCOMAddin");
oStandardBar = null;
oCommandBars = null;
}
/// <summary>
/// Implements the OnBeginShutdown method of the IDTExtensibility2
interface.
/// Receives notification that the host application is being unloaded.
/// </summary>
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;
}
private void MyButton_Click(CommandBarButton cmdBarbutton, ref bool
cancel)
{
///... My action code
}
private object applicationObject;
private object addInInstance;
}
}
I have created an addin for word that when loaded adds a button
to the Standard Tool Bar when MS Word is opened.
However each time I open MSWord a new instance of the button is added
to the toolbar.
Can anyone advise how I can prevent this.
A snippet of my code is attached, this is based on code from the following
Url:
http://www.rootsilver.com/2007/08/why-doesnt-my-custom-office-ad.html.
Thanks for any assistance
Regards Andrew
namespace MyReportSaver
{
using System;
using Extensibility;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Reflection;
using Microsoft.Office.Core;
/// <summary>
/// The object for implementing an Add-in.
/// </summary>
/// <seealso class='IDTExtensibility2' />
[GuidAttribute("8A7B4843-1B92-41D0-8A0C-FFE4E55E1E25"),
ProgId("MyReportSaver.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{
private CommandBarButton MyButton;
/// <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>
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>
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>
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>
public void OnStartupComplete(ref System.Array custom)
{
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["My
Custom Button"];
//MyButton.Visible = false;
}
catch (Exception)
{
object omissing = System.Reflection.Missing.Value;
MyButton =
(CommandBarButton)oStandardBar.Controls.Add(1, omissing,
omissing,
omissing, omissing);
MyButton.Caption = "My Custom Button";
}
MyButton.Tag = "My Custom Button";
MyButton.OnAction = "8A7B4843-1B92-41D0-8A0C-FFE4E55E1E25";
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);
// Display a simple message to show which application you
started in.
//System.Windows.Forms.MessageBox.Show("This Addin is loaded by
" + oName.ToString(), "MyCOMAddin");
oStandardBar = null;
oCommandBars = null;
}
/// <summary>
/// Implements the OnBeginShutdown method of the IDTExtensibility2
interface.
/// Receives notification that the host application is being unloaded.
/// </summary>
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;
}
private void MyButton_Click(CommandBarButton cmdBarbutton, ref bool
cancel)
{
///... My action code
}
private object applicationObject;
private object addInInstance;
}
}