none
Wrong context is given to a Ribbon button in Excel (scenario supplied) RRS feed

  • Question

  • Hello,

    Create a new Excel 2013-2015 VSTO add-in project. Add the the following Ribbon XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
      <ribbon>
        <tabs>
          <tab idMso="TabAddIns">
            <group id="Group1" label="Group1">
              <button id="button1" getLabel="GetLabel"/>          
            </group>
          </tab>
        </tabs>
      </ribbon>
    </customUI>

    Add the following code to Ribbon1.cs:

            public string GetLabel(Office.IRibbonControl control) {
                string result = "(unknown)";
                object context = control.Context;
                if (context is Excel.Window) {
                    Excel.Window window = context as Excel.Window;
                    result = window.Caption;
                }
                return result;
            }
            public void InvalidateControl(string id) {
                this.ribbon.InvalidateControl(id);
            }

    Add the following to ThisAddIn.cs:

            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                this.Application.WindowActivate += Application_WindowActivate;
            }
            void Application_WindowActivate(Excel.Workbook Wb, Excel.Window Wn) {
                string message = "!!! " + Wb.Name + " " + Wn.Caption;
                System.Diagnostics.Debug.WriteLine(message);
                ribbon1.InvalidateControl("button1");
            }
    
            Ribbon1 ribbon1 = null;
            protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject() {
                ribbon1 = new Ribbon1();
                return ribbon1;
            }
    Build the add-in, open two workbooks and make the ribbon tab active. Everything goes as expected if you activate the Excel windows by clicking on the window caption area: the buttons show the caption of the corresponding Excel window on them. If however you activate an Excel window by clicking an arbitrary cell, the Ribbon buttons on both active and inactive windows get updated with the same context; accordingly they display the same caption which is wrong. I reproduce this issue on Excel 2013 and 2016. I use VS 2013 with Update 5. I'm ready to provide extra info if required.


    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader

    Please mark answers and useful posts to help other developers use the forums efficiently.

    Friday, April 22, 2016 1:06 PM

Answers

All replies

  • Andrei,

    Does it work correctly in Outlook?

    Anyway, the forum is for Outlook specific questions. I'd suggest asking Excel specific questions on the Excel for Developers forum. 


    [custom.development]

    Friday, April 22, 2016 1:46 PM
  • Oops! Could someone move the topic to the corresponding group please?


    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader

    Please mark answers and useful posts to help other developers use the forums efficiently.

    Friday, April 22, 2016 1:57 PM
  • Hi Andrei Smolin,

    I follow your mentioned steps to reproduce this issue on my side but I did not reproduce your issue.

    Every time it shows the correct caption.

    I get the same result when I click on Caption area and by clicking on cell.

    please correct me if I miss a step in between.

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, April 25, 2016 12:33 AM
    Moderator
  • I'm sorry for the delayed response; I'm on vacations at the moment. Here's what I get in Excel 2013. When at work, I reproduce exactly the same behavior in Excel 2016.



    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader

    Please mark answers and useful posts to help other developers use the forums efficiently.

    Friday, April 29, 2016 3:38 PM
  • Hi Andrei Smolin,

    I have again make a test. please see the result below.

    we get a same output and it looks like this output is correct based on the line below.

     
    string message = "!!! " + Wb.Name + " " + Wn.Caption;
    

    output I have get on the label below.

    it also looks correct.

    I never get same label on different worksheet.

    still I am not able to reproduce your issue at my side.

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, May 2, 2016 9:14 AM
    Moderator
  • Hi Andrei Smolin,

    Finally I am able to reproduce your issue at my side. I also get same behavior of Excel like you.

    see the image below.

    it looks like its a bug in Excel.

    you can give a feedback regarding this issue to Excel User Voice.

    To give your feedback to User Voice please visit link below.

    Excel User Voice

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, May 2, 2016 9:23 AM
    Moderator
  • Hello Deepak,

    Thank you for letting me know about your findings. As to User Voice, your suggestion doesn't seem applicable because I'm a developer. My only goal is to let the Excel team know about the bug.


    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader

    Please mark answers and useful posts to help other developers use the forums efficiently.

    Tuesday, May 3, 2016 11:09 AM
  • Hi Andrei Smolin,

    yes from above results now we know about this issue in excel.

    Thanks for share this information with us. other community members can also get information from your post.

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, May 4, 2016 1:36 AM
    Moderator