T
Tommie Johansson
Hi!
I have a problem with word addins. When I create a custom CommandBar with
CommandBarButtons (yes, I define them at class level so the garbage collector
won't kill em) they work fine, until I click "New document" toolbar button.
The events wont fire when the "second" word app starts.
I've tried the following code ( posted by Wei Lu [MSFT] in response to the
"CommandBarButton click event does not fire" 1/17/2007).
Can someone please at least try the code and see if you get the same error??
thanks
/Tommie
using System;
using System.Windows.Forms;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
namespace WordAddin2
{
public partial class ThisApplication
{
private Office.CommandBar AddInMenuBar;
private Office.CommandBarButton OpenTaskPaneButton;
private void ThisApplication_Startup(object sender,
System.EventArgs e)
{
try
{
AddInMenuBar =
this.ActiveWindow.Application.CommandBars.Add(
"Amazon", missing, missing, true);
OpenTaskPaneButton = (Office.CommandBarButton)
(AddInMenuBar.Controls.Add(
Office.MsoControlType.msoControlButton,
missing, missing, missing, true));
OpenTaskPaneButton.Caption = "Search Amazon";
OpenTaskPaneButton.Style =
Microsoft.Office.Core.MsoButtonStyle.msoButtonCaption;
OpenTaskPaneButton.Click += new
Office._CommandBarButtonEvents_ClickEventHandler(
OpenTaskPaneButton_Click);
AddInMenuBar.Visible = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ex.Source,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void OpenTaskPaneButton_Click(Office.CommandBarButton Ctrl,
ref bool CancelDefault)
{
MessageBox.Show("The button has been clicked");
}
private void ThisApplication_Shutdown(object sender,
System.EventArgs e)
{
OpenTaskPaneButton.Delete(false);
OpenTaskPaneButton = null;
AddInMenuBar = null;
}
#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(ThisApplication_Startup);
this.Shutdown += new
System.EventHandler(ThisApplication_Shutdown);
}
#endregion
}
}
I have a problem with word addins. When I create a custom CommandBar with
CommandBarButtons (yes, I define them at class level so the garbage collector
won't kill em) they work fine, until I click "New document" toolbar button.
The events wont fire when the "second" word app starts.
I've tried the following code ( posted by Wei Lu [MSFT] in response to the
"CommandBarButton click event does not fire" 1/17/2007).
Can someone please at least try the code and see if you get the same error??
thanks
/Tommie
using System;
using System.Windows.Forms;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
namespace WordAddin2
{
public partial class ThisApplication
{
private Office.CommandBar AddInMenuBar;
private Office.CommandBarButton OpenTaskPaneButton;
private void ThisApplication_Startup(object sender,
System.EventArgs e)
{
try
{
AddInMenuBar =
this.ActiveWindow.Application.CommandBars.Add(
"Amazon", missing, missing, true);
OpenTaskPaneButton = (Office.CommandBarButton)
(AddInMenuBar.Controls.Add(
Office.MsoControlType.msoControlButton,
missing, missing, missing, true));
OpenTaskPaneButton.Caption = "Search Amazon";
OpenTaskPaneButton.Style =
Microsoft.Office.Core.MsoButtonStyle.msoButtonCaption;
OpenTaskPaneButton.Click += new
Office._CommandBarButtonEvents_ClickEventHandler(
OpenTaskPaneButton_Click);
AddInMenuBar.Visible = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ex.Source,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void OpenTaskPaneButton_Click(Office.CommandBarButton Ctrl,
ref bool CancelDefault)
{
MessageBox.Show("The button has been clicked");
}
private void ThisApplication_Shutdown(object sender,
System.EventArgs e)
{
OpenTaskPaneButton.Delete(false);
OpenTaskPaneButton = null;
AddInMenuBar = null;
}
#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(ThisApplication_Startup);
this.Shutdown += new
System.EventHandler(ThisApplication_Shutdown);
}
#endregion
}
}