How can I disable Word's built-in Ribbon Tabs when a specific document is loaded?

独自空忆成欢 提交于 2021-02-10 20:20:30

问题


I am using the following code right now:

<?xml version="1.0" encoding="UTF-8"?>
<customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2006/01/customui">
<commands>
<command idMso="Font" enabled="false"/>
<command idMso="AlignLeft" enabled="false"/>
<command idMso="AlignCenter" enabled="false"/>
<command idMso="AlignRight" enabled="false"/>
</commands>

and so on, but this completely disables the buttons obviously. I now want to restrict my users from being able to use certain built-in Word controls when a specific document is loaded, and leave them with the liberty of doing whatever the rest of the time. I have tried to use:

<command idMso="FontSize" getEnabled="checkDisable"/>

where checkDisable checks for what documents are open. That document is then searched for a string (that's how I know I want to disable the buttons), but the checkDisable callback fires before the document is actually loaded. Can I somehow directly disable specific buttons from the codebehind instead of just via XML? If I could do this, I could disable the buttons on the DocumentLoaded event.

ANY suggestions are appreciated, I've been banging my head against the wall Googling this one for hours.


回答1:


You are on the right avenue. You need to use callbacks instead of attributes. But Office caches the values and don't call the callbacks each time as you want it. Instead, to refresh the UI (force Office application to invoke callbacks) you need to use the Invalidate or InvalidateControl methods of the IRibbonUI interface. See Overview of the IRibbonUI Object for more information.

For example, when you open a new document in the host application the Open event is fired. You can check out the Document and call the Invalidate method (if required) to force the host application to call your callbacks and therefore refresh the UI.



来源:https://stackoverflow.com/questions/28882410/how-can-i-disable-words-built-in-ribbon-tabs-when-a-specific-document-is-loaded

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