none
CommandbarButton not working for Text menu RRS feed

  • Question

  • Hello, I have the same problem, but it still doesn't work, can you show me a sample code?
    • Split by Cindy Meister MVPModerator Thursday, July 9, 2015 2:01 PM Split from older, answered question: https://social.msdn.microsoft.com/Forums/vstudio/en-US/0e0ef1af-7f3b-405b-a407-0a77d4a8758e/officecommandbarbutton-click-event-not-firing-when-clicked?forum=vsto
    Tuesday, July 7, 2015 8:58 PM

Answers

All replies

  • Hi Bitterman

    Taking the code example at the top in the first message of this thread and extracting a sample portion to illustrate. Here's the original:

            public void addDeptContact(TWUserGroupInfo thisDept,int idx)
            {
                int ContactLen = thisDept.ModelUsers.Length;
    
                Office.CommandBarPopup thispop;
                thispop = (Microsoft.Office.Core.CommandBarPopup)pop_contacts.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlPopup, missing, missing, missing, missing);
                thispop.Caption = thisDept.Deparment + "(" + ContactLen + ")";
                thispop.BeginGroup = false;
                thispop.Enabled = true;
    

    And here's the corrected extract; note WHERE the DECLARATION is, now:

            Office.CommandBarPopup thispop;
    
            public void addDeptContact(TWUserGroupInfo thisDept,int idx)
            {
                int ContactLen = thisDept.ModelUsers.Length;
    
                thispop = (Microsoft.Office.Core.CommandBarPopup)pop_contacts.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlPopup, missing, missing, missing, missing);
                thispop.Caption = thisDept.Deparment + "(" + ContactLen + ")";
                thispop.BeginGroup = false;
                thispop.Enabled = true;
    


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, July 8, 2015 2:09 PM
    Moderator
  • But when I must add the event handler?

    In my case I use a button in the Text Command Bar:

        public partial class ThisAddIn
        {
            private Office.CommandBar bar;
            private Office.CommandBarButton button;
    
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                bar = Application.CommandBars["Text"];
                bar.Reset();
    
                button = (Office.CommandBarButton)bar.Controls.Add();
                button.accName = "My Button";
                button.Caption = "My Button";
                button.Click += new Office._CommandBarButtonEvents_ClickEventHandler(CommandBarButton_Click);
            }
    
            private void CommandBarButton_Click(Office.CommandBarButton sender, ref bool Cancel)
            {
                MessageBox.Show("My Button clicked!");
            }
        }
    Declaring the bar and the button still doesn't work.

    Wednesday, July 8, 2015 4:36 PM
  • Hi Bitterman

    The event appears to be in a valid place, inside ThisAddin_Startup.

    It appears you're trying to work with a Right-Click menu. Which version of Office/Word do you have installed?

    Are you seeing the button? But nothing is happening when you click it?


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, July 9, 2015 1:58 PM
    Moderator
  • I've split this from

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/0e0ef1af-7f3b-405b-a407-0a77d4a8758e/officecommandbarbutton-click-event-not-firing-when-clicked?forum=vsto

    to a new thread for better visibility and a shorter discussion "thread" :-)


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, July 9, 2015 2:03 PM
    Moderator
  • Hello Cindy,

    I am using Visual Studio 2013 and Word 2013, the Office.dll is version 15.

    The button appears, but nothing happens when clicked.

    Thursday, July 9, 2015 6:36 PM
  • Hi Ricardo

    Starting with Office 2010, context menus have been included in the Ribbon XML customization. In some instances, the CommandBars interface still works, but with Office 2013 you should definitely be doing this via Ribbon XML.

    The VSTO documentation on adding a Ribbon XML part to you solution:
    https://msdn.microsoft.com/en-us/library/aa942866.aspx

    For definging context menus in the Ribbon XML
    Customize context menus in Office 2010


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by L.HlModerator Sunday, July 19, 2015 7:11 AM
    Friday, July 10, 2015 8:19 PM
    Moderator
  • Hi Cindy,

    Thanks for the answers, now I can execute something on the button click.

    But I have another problem too, how can I access my created buttons in runtime? Like "ThisAddIn_Startup" for example.

    I need to hide the buttons if the addin is not started.

    Tuesday, July 21, 2015 6:19 PM
  • Hi Ricardo

    Mmm. Normally, I'd expect you to have defined the Ribbon as part of your Add-in, so the buttons would only show when your add-in is loaded. Have you done it some other way?


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, July 22, 2015 3:18 PM
    Moderator
  • Yes, I did it. But I want to hide the buttons in the ThisAddIn_Startup and before Application_WindowBeforeRightClick, but defining these in the Ribbon I have no idea how to reach the properties like Visible of the Button.

    I tried to access in Application.CommandBars, but is not there.
    Wednesday, July 22, 2015 4:47 PM
  • Hi Ricardo

    Did you use the Ribbon Designer or do you have a Ribbon XML type of solution?


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, July 23, 2015 4:36 PM
    Moderator
  • I used Ribbon XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
      <contextMenus>
        <contextMenu idMso="ContextMenuText">
          <button id="MyButton" label="My Button" onAction="GetButtonID" />
        </contextMenu>
      </contextMenus>
    </customUI>

    Thursday, July 23, 2015 4:43 PM
  • Hi Ricardo

    OK, first documentation/references for working with Ribbon XML:
    https://social.msdn.microsoft.com/Forums/office/en-US/ba8c9d0b-7312-4503-8167-f2ef86f17435/information-on-the-office-fluent-ui-ribbon-for-developers?forum=officegeneral

    You'll find a list there and for getting started, the three-part article on customizing the 2007 Ribbon for developers is what you want to read.

    While it's important to have an idea about all the information it presents, relevant for this particular question is how to use "callbacks" to get dynamic behavior, such as making a control visible or enabled.

    The button control has a getVisible attribute, to which you assign the name of a procedure defined in the Ribbon1 (that's the default name, you may have given it another name when you inserted the Ribbon into your project) class. This procedure needs to have the correct method signature - you'll find a table of method signatures for the various callbacks in Part 3 of the article. This method takes care of telling the button whether Visible is on/off (true/false) according to validation criteria you specify.

    These callbacks are triggered automatically when the Ribbon loads and provide the initial state of the Ribbon and its controls.

    Later, when some sort of action should change the state of a control, your code needs to issue Ribbon.InvalidateControl("controlName") in order to execute the callbacks specified in the RIbbon XML for the control.

    All this is covered in the articles: my explanation is simply to give you an idea how it works so that you know what to pay special attention to.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, July 24, 2015 3:20 PM
    Moderator
  • Thanks Cindy, now it works fine!
    Friday, July 24, 2015 6:28 PM