Hi,
I have created a COM add-in for Word. When I start word the plugin works fine, but at the moment that I click "New Document" and Document2 is opened then the plugin no longer fires the click event. My code is as follows:
namespace
MyAddin2{
using System; using Extensibility; using System.Runtime.InteropServices; using System.Windows.Forms; using Word = Microsoft.Office.Interop.Word; using Microsoft.Office.Core; [GuidAttribute("E782ADF5-EF2A-446E-A5AF-8C0E171C0816"), ProgId("MyAddin2.Connect")] public class Connect : Object, Extensibility.IDTExtensibility2{
private Microsoft.Office.Interop.Word.Application applicationObject; private Microsoft.Office.Core.COMAddIn addInInstance; private CommandBarButton simpleButton; private object missing = System.Reflection.Missing.Value; public Connect(){
}
public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom){
applicationObject = (Microsoft.Office.Interop.Word.
Application)application;addInInstance = (Microsoft.Office.Core.
COMAddIn)addInInst;}
public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom)
{
//MessageBox.Show("ondisconnection");
}
public void OnAddInsUpdate(ref System.Array custom){
//MessageBox.Show("onaddinsupdate");}
public void OnStartupComplete(ref System.Array custom){
CommandBars commandBars; CommandBar standardBar;commandBars = applicationObject.CommandBars;
// Get the standard CommandBar from WordstandardBar = commandBars[
"Standard"]; try{
// try to reuse the button is hasn't already been deletedsimpleButton = (
CommandBarButton)standardBar.Controls["Word Addin"];}
catch (System.Exception){
// If it's not there add a new buttonsimpleButton = (
CommandBarButton)standardBar.Controls.Add(3, missing, missing, missing, missing);simpleButton.Caption =
"Word Addin";simpleButton.Style =
MsoButtonStyle.msoButtonCaption;}
// Make sure the button is visiblesimpleButton.OnAction =
"!<MyAddin2.Connect>";simpleButton.Visible =
true;simpleButton.Click +=
new _CommandBarButtonEvents_ClickEventHandler(simpleButton_Click);standardBar =
null;commandBars =
null;}
public void OnBeginShutdown(ref System.Array custom){
//MessageBox.Show("onbeginshutdown");}
void simpleButton_Click(CommandBarButton ctrl, ref bool cancelDefault){
MessageBox.Show("You clicked on the button");}
}
}
The simpleButton is created in class scope, so I can't see why there should be a memory leak. Any help

My Word COM add-in stops working - I do not think it is a memory leak
Alexandre Poisson
Hi Mike,
I do not think it is a memory leak. I have used the Extensible wizard and added a shared add-in, selected Word and have the add-in registered in HKEY_Current_User.
I load the new document by clicking the new button on the standard command bar. After doing this the add-in stops responding to click events. The add-in is still loaded because when I close Word I get message boxes from the "shut down events".
I use a add-in because I need some global functionality, and I have not found a way to do that using a template that automatically loads when Word is started.
Shingie
Hi
Why do you think there is a memory leak, did you use an Extensible wizard as I note this is application based and not VSTO Template based.
The important thing is i presume that the event for the command bar is not firing when you load a new document How do you load the new document is that from within the Word UI or from an icon in explorer
Regards
CHASE10541
Hi,
Your addin is failing in OnStartupComplete() method. The presence of the "Word Addin" button in the toolbar may have led you to believe that your addin was loaded correctly. But once a button is added to Word by an addin at application level, it is permanently there, whether any addins load correctly or not.
Following line in OnStartupComplete method of your addin will throw an invalid cast exception (E_NOINTERFACE from Word):
simpleButton = (CommandBarButton)standardBar.Controls.Add(3, missing, missing, missing, missing);
Reason
You are passing the interger 3 as value for Office.MsoControlType enum parameter in Add method. This corresponds to msoControlDropDown control NOT msoControlButton control, as you can see from the definition of Office.MsoControlType enum below:
typedef enum {
So, when you try to cast the object returned by standardBar.Controls.Add() to a Office.CommandBarButton it naturally fails.msoControlCustom = 0,
msoControlButton = 1,
msoControlEdit = 2,
msoControlDropdown = 3,
.......
} MsoControlType;
My suggestion is not to write code that passes intergers for enum parameters:
simpleButton = (
CommandBarButton)standardBar.Controls.Add(MsoControlType.msoControlButton, missing, missing, missing, missing);I tried the addin with this fix and it seems to work okay. Hopefully this solves your issue.