MSWord addin error

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;
}
}
 
S

SvenC

Hi Andrew,
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.

You can add you button as temporary button. So you can savely add it
whenever an Office App is started.

It is the last parameter of the Add method IIRC.
 
P

Pete

Actually the behaviour in Word is slightly different than in Excel &
PowerPoint. The Temporary parameter of the Add method doesn't stop the new
button being cached in the current template (according to the
CustomizationContext) and appearing on the toolbar next time the app starts.
You have to either specifically delete the button on shutdown or check on
startup if it already exists (by name) and then either delete it and
re-create it just set a reference to it and add an event handler.

It's better to set Temporary to False and allow the button to become
permanently embedded in the template.

Pete
 
A

Andrei Smolin [Add-in Express]

Hello Andrew,

You should add/reconnect your command bars and controls in the
WindowActivate event handler of Word application. Be aware, the button may
become disconnected all of a sudden. That is, you should be ready to
re-connect it. You should also take care of your command bars and buttons
not being shown in Outlook e-mail inspectors when Word is set as the default
mail editor.

Regards from Belarus (GMT+2),

Andrei Smolin
Add-in Express Team Leader
www.add-in-express.com

Andrew Mercer said:
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;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top