How to intercept clicking of a built-in Office Ribbon control

淺唱寂寞╮ 提交于 2019-12-31 01:59:05

问题


I'm wondering if it's possible to detect when a user has clicked the Header/Footer button in Excel so I can show some custom header/footer related ribbon controls on my add-in's tab and hide them when the user is not in header/footer edit mode.

Is it possible to hijack this button click somehow? I've seen applications hijack the Excel Save before. I'm looking for similar behavior just with the header/footer button.

I'm using C#, Visual Studio 2012 and Excel 2010. I've created my custom ribbon using the Ribbon XML approach.


回答1:


One way I've discovered that worked was to use the <commands> section in the Ribbon XML (which I didn't know existed.) Apparently this mechanism allows you to re-purpose actions intrinsic to Excel but beware that not all controls support re-purposing the onAction callback)

<?xml version="1.0" encoding="UTF-8"?>
<customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2006/01/customui">
    <commands>
        <command idMso="HeaderFooterInsert" onAction="testHeaderFooter"/>
    </commands>
  <ribbon>
    <tabs>...

along with the associated event handler:

    public void testHeaderFooter(Office.IRibbonControl control, bool cancelDefault)
    {
        MessageBox.Show("Testing.");
        cancelDefault = false;
    }

This link was very helpful:

http://social.msdn.microsoft.com/Forums/office/en-US/e1a60d16-053e-4697-b17c-b22d602f0400/intercept-the-onaction-event-of-a-gallery-element-of-excel-2007-ui-ribbon?forum=vsto



来源:https://stackoverflow.com/questions/21172103/how-to-intercept-clicking-of-a-built-in-office-ribbon-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!