Search code examples
c#xmlvstooutlook-addinribbon

VSTO how to populate XML ribbon combobox on load?


I am working on trying to load a list of items (preferably from a file like csv, etc.) and fill in a combobox with those items. Then I want to be able to grab the text of the selected item. I have routinely done this fine in other C# projects, but it seems it does not work the same way in VSTO xml. Any assistance would be appreciated.

Edit to add code... I found the RibbonDropDownItem interface which it seems is required to add an item, but the C# below wont work because it cant find the cboItems object in the XML.

<?xml version="1.0" encoding="UTF-8"?>
<customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2006/01/customui">
    <ribbon>
        <tabs>
            <tab idMso="TabNewMailMessage">
                <group id="group1" label="Work Orders">
                    <dropDown id="cboItems" label="Select a work order:"
                               getItemLabel="GetItemLabel"
                               getItemCount="GetItemCount"
                               onAction="OnSendUsingAction"
                               >
                    </dropDown>
                    <button id="btnSelect" label="Submit" 
                     onAction="ButtonClick"/>
                </group>
            </tab>
        </tabs>
    </ribbon>
</customUI>

private List<string> workOrders = new List<string>();
public void Ribbon_Load(Office.IRibbonUI ribbonUI)
    {
        this.ribbon = ribbonUI;
    }

    public void OnSendUsingAction(Office.IRibbonControl control, string value, int index)
    {
        if (value == "cboItems")
            selectedWorkOrder = workOrders[index];
    }

    public void ButtonClick(Office.IRibbonControl control)
    {
        MessageBox.Show(selectedWorkOrder);
    }

    public string GetItemLabel(Office.IRibbonControl control, int index)
    {
        return workOrders[index];
    }

    public int GetItemCount(Office.IRibbonControl control)
    {
        return workOrders.Count();
    }

Solution

  • For combobox with variable number of items, you need to specify getItemCount / getItemLabel / getSelectedItemIndex / onAction callbacks:

    <dropDown id="someId" label="some label"
                   showLabel="false" 
                   getItemImage="GetItemImage"
                   getItemCount="GetItemCount"
                   getItemLabel="GetItemLabel"
                   getSelectedItemIndex="GetSelectedItemIndex"
                   onAction="OnSendUsingAction"
                   sizeString= "Some long string to size the control"
                 >