I'm writing an MS Project 2007 Add-in (VS 2010, WinXP) that creates a toolbar w/button and assigns a HelloWorld onclick eventhandler to said button.
After being installed, the plugin creates the button, wires up the click event, and everything works just fine. However, after a few minutes, the onclick event inexplicably stop firing.
Everything I've read says that I need to define my toolbar/button in the global scope, which I've done. However, the onclick event still gets unhooked after a few minutes of runtime.
One other weird symptom I'm experiencing is that when I toggle it in the COM Add-ins Dialog box (after its stopped working), I get this weird message:
"com object that has been seperated from its underlying RCW cannot be used"
...which is strange, because in this simple application, I'm not releasing any COM objects.
Any suggestions?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using MSProject = Microsoft.Office.Interop.MSProject;
using Office = Microsoft.Office.Core;
namespace Test_Project2007_Addin
{
public partial class ThisAddIn
{
private Office.CommandBar cmdBar; // Hello World Toolbar
private Office.CommandBarButton cmdBtn01; // Hellow World Button
private string cmdBarName = "Hello World Toolbar";
private string cmdBtn01Name = "HelloWorld";
private void ThisAddIn_Startup( object sender, System.EventArgs e ) {
// Define the toolbar
cmdBar = this.Application.CommandBars.Add(
cmdBarName, Office.MsoBarPosition.msoBarTop, false, true );
cmdBar.Visible = true;
// Define the button
cmdBtn01 = cmdBar.Controls.Add( Office.MsoControlType.msoControlButton, missing, missing, missing, true ) as Office.CommandBarButton;
cmdBtn01.FaceId = 422;
cmdBtn01.Caption = "Hello World";
cmdBtn01.Tag = cmdBtn01Name;
cmdBtn01.DescriptionText = "Hello World";
cmdBtn01.TooltipText = "Hello World";
cmdBtn01.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler( HelloWorld );
}
private void ThisAddIn_Shutdown( object sender, System.EventArgs e ) {
}
private void HelloWorld( Microsoft.Office.Core.CommandBarButton barButton, ref bool someBool ) {
System.Windows.Forms.MessageBox.Show( "Hello, World!" );
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}
Try adding the CommandBars
as a private member variable. Maybe CommandBars
is getting garbage collected. See if this fixes your RCW issue - if not it may be another plugin.
If this doesn't work, maybe try making Application
a local member. Sorry - I don't have MS Project to test this out.
private CommandBars cmdBars; // app command bars
private void ThisAddIn_Startup( object sender, System.EventArgs e ) {
//..
cmdBars = this.Application.CommandBars;
cmdBar = cmdBars.Add(cmdBarName, Office.MsoBarPosition.msoBarTop, false, true );
//..
}