RE: [ExcelDna] Issue with drop down item in ribbon

1,692 views
Skip to first unread message

Govert van Drimmelen

unread,
Sep 20, 2012, 5:33:57 AM9/20/12
to exce...@googlegroups.com
Hi Amael,

The Office ribbon is quite finicky.
You have to get the callback signatures just right - in general they don't take 'ref' parameters, but instead return the relevant value. I check against this list of signatures, which seems to be the best and most complete documentation I can get: http://msdn.microsoft.com/en-us/library/office/aa722523(v=office.12).aspx

Further, you have to be really careful with the attribute names. E.g. you need to ust getItemID instead of getItemId, else the ribbon won't load.

I paste below a complete .dna file, which contains the ribbon definition and C# callbacks.
You just need to add an ExcelDna.xll copy to see it work.

Please write back if you need any help getting it to work.

Regards,
Govert


<DnaLibrary Name="Ribbon Tests" Language="C#">
<Reference Name="System.Windows.Forms" />
<![CDATA[
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using ExcelDna.Integration;
using ExcelDna.Integration.CustomUI;

// Can make a class that implements ExcelDna.Integration.CustomUI.ExcelRibbon
// to get full Ribbon access.
[ComVisible(true)]
public class MyRibbon : ExcelRibbon
{
    public void OnButtonPressed(IRibbonControl control)
    {
        Console.Beep();
        MessageBox.Show("My Button Pressed on control " + control.Id);
    }
    
    public int GetItemCount(IRibbonControl control)
    {
        Console.Beep();
        Console.Beep();
        return 3;
    }
  
    public string GetItemID(IRibbonControl control,int index)
    {
        return "wB" + index;
    }
    
    public string GetItemLabel(IRibbonControl control,int index)
    {
        string label= "Unknow Workbook";
 
        if (index==1) label= "1 Workbook";
        if (index==2) label= "S. Workbook";
        if (index==3) label= "A. Workbook";
        //_myRibbon.InvalidateControl("cbScenarioWorkbook");
        return label;
    }
    
    public void SaveChoice(IRibbonControl control, string selectedId, int selectedIndex)
    {
        MessageBox.Show("My Dropdown Selected on control " + control.Id + " with selection " + selectedId + " at index " + selectedIndex);
    }
}
]]>
<CustomUI>
      <ribbon>
        <tabs>
          <tab id='CustomTab' label='My Ribbon Test Tab'>
            <group id='SampleGroup' label='My Ribbon Test Group'>
              <button   id='TestButton' 
                        label='My Test Button' 
                        onAction='OnButtonPressed'/>
              <dropDown id='cbScenarioWorkbook'                         
                        label='Workbook' 
                        getItemCount='GetItemCount' 
                        getItemID='GetItemID' 
                        getItemLabel='GetItemLabel' 
                        onAction='SaveChoice' />
            </group >
          </tab>
        </tabs>
      </ribbon>
    </customUI>
  </CustomUI>
</DnaLibrary>





From: exce...@googlegroups.com [exce...@googlegroups.com] on behalf of Amael Noel [noel....@gmail.com]
Sent: 19 September 2012 07:04 PM
To: exce...@googlegroups.com
Subject: [ExcelDna] Issue with drop down item in ribbon

Hi,
 
I've included a dropdown inside my ribbon. it appears when I open Excel but it's empty. I've create the following functions :
 

public void GetItemCount(IRibbonControl control,ref int count)

{
count = 3;
}
public void GetItemId(IRibbonControl control,int index,ref string id)
{
id =
"wB" + index;
}

     

public void GetItemLabel(IRibbonControl control,int index,ref string label){

label=
"Unknow Workbook";
 
if (index==1) label= "1 Workbook";
if (index==2) label= "S. Workbook";
if (index==3) label= "A. Workbook";
_myRibbon.InvalidateControl("cbScenarioWorkbook");

The part of the code for inside the ribbon.xml is the following :

<dropDown id="cbScenarioWorkbook" label="Workbook" onAction="SaveChoice" getItemCount="GetItemCount"getItemID="GetItemId" getItemLabel="GetItemLabel" showLabel ="true" >

<item id="item1" label="Man" />

<item id="item2" label="World" />

</dropDown>

The dropDown is always empty inside Excel. Could you help me how to solve this issue.

Many thanks,
 
Ama

--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To view this discussion on the web visit https://groups.google.com/d/msg/exceldna/-/fIuhK4Ed1pIJ.
To post to this group, send email to exce...@googlegroups.com.
To unsubscribe from this group, send email to exceldna+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/exceldna?hl=en.

Chrissy

unread,
Jun 14, 2013, 3:30:17 PM6/14/13
to exce...@googlegroups.com
Hi Govert, 

This works fine if I already know how many items should be on the dropdown list and what they are. What if I need to get a list of filenames from a specific directory? I can get the number of files in the directory so the getting count is fine, what I'm unsure of is how to assign the index with the label; where the index will be 0 to the count - 1 and the label will be the file names in the directory. :( 

I've tried something like 
public string GetItemLabel(IRibbonControl control,int index)
    {
        //string[] fileList = Directory.GetFiles(@"my specified drive");
        var fileNames = Directory.EnumerateFiles(@"myspecified drive", "*", SearchOption.AllDirectories).Select(Path.GetFileName);   
        string label = "Templates";
        foreach (string fName in fileNames)
        {
            label = fName;
            index++;
        }
        return label;
}

Also, I had the custom ribbon and everything working well yesterday but when I reinstalled the add-in today, the ribbon no longer shows. :( All I did was add another group under the same tab. Any idea what would have caused such a problem? Could it be that when I reinstalled the add-in, the original one did not uninstall properly and is causing problems now?

Thank you in advance for your help. :)

Govert van Drimmelen

unread,
Jun 14, 2013, 3:56:52 PM6/14/13
to Excel-DNA
Hi,

Excel will not load the ribbon if there is any error in the xml, for
example upper/lower case attributes. This is the most likely problem.
Try to get back to a simple working version.

You can also check that Excel has not added you to the Disabled Items
(under File -> Options -> Add-ins -> Manage: Disabled Items).
Otherwise you might have to check that the basic samples in the
distribution work, and work from there.

For the list I suggest you investigate the ribbon links from here:
https://exceldna.codeplex.com/wikipage?title=Ribbon%20Customization

I don't think you assign the index - you should set up the internal
data, then call some kind of Invalidate function that causes the
getItemCount callback to be called, and then getItemLabel for each
item.

Like the first post in this discussion said:
"The Office ribbon is quite finicky."

Regards,
Govert


* * * * * * * * * * * * * * * * * * * * * * * * * * * *

Ensure that the Excel-DNA project continues by
making your donation - http://excel-dna.net/support/

* * * * * * * * * * * * * * * * * * * * * * * * * * * *

Angela Hornung

unread,
Jan 29, 2024, 12:38:44 PM1/29/24
to Excel-DNA
Curious, is there a way to simply give the dropdown an array of items as an argument and simply define two functions, one which returns the array of items and the other which reviews the user's choice and determines what to do? The above mentioned method does work and is fantastic if the quantity of items is dynamic, but I have a set list of items.
Reply all
Reply to author
Forward
0 new messages