简体   繁体   中英

VSTO Excel preserve Ribbon state

I have simple VSTO Excel 2013 Application level Add-in with custom Ribbon, which includes toggle button and checkbox. If I open two files (workbooks) I can see that the Ribbons do not preserve their state across multiple windows, meaning if I click on checkbox or toggle button on Second workbook the same checkbox state is shown on a first workbook and vise versa. I found an article which describes a similar situation for outlook : https://www.add-in-express.com/creating-addins-blog/2013/01/30/preserve-outlook-ribbon-controls-state/ but unfortunately the Inspector window event is not available in Excel. Any idea on how to deal with it?

You need to use callbacks instead of attributes in the Ribbon XML. Also when a user changes the active window you need to call the Invalidate/InvalidateControl method of the IRibbonUI interface to force Office applications (Excel in your case) call your callbacks for the current state of controls. It's pretty easy...

You can read more about the Ribbon UI (aka Fluent UI) in the following series of articles in MSDN:

Also you may find the following ones helpful:

I tried a sample with a toggle button with toggle on and then switched to another workbook the toggle button doesnt persist. But then if you stored the pressed value in a variable and return that on getPressed callback of a toggle button it worked.

Ribbon.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="TabAddIns">
                <group id="group1" label="group1">
                    <toggleButton id="toggleButton1" label="toggleButton1" size="large" getPressed="buttonPressed" onAction="buttonAction"/>
                </group>
            </tab>
        </tabs>
    </ribbon>
</customUI>

Ribbon.cs

private bool isButtonPressed = false;
    public void buttonAction(Office.IRibbonControl control, bool isPressed)
    {
        isButtonPressed = isPressed;
    }
    public bool buttonPressed(Office.IRibbonControl control)
    {
        return isButtonPressed;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM