VSTO:仅在加载文档时启用功能区按钮 [英] VSTO: Enable ribbon button only when a document is loaded

查看:62
本文介绍了VSTO:仅在加载文档时启用功能区按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 Word 插件中的功能区按钮设置为在加载文档时启用并在未加载文档时禁用,就像大多数内置按钮一样?

How does one set a ribbon button in a Word add-in to be enabled when a document is loaded and disabled when no documents are loaded, just Like most of the built-in buttons?

可以将全局标志绑定到按钮的启用"属性,还是比这更复杂?我知道我可以创建一个定时循环来检查 Application.Documents 中的更改,但如果可能的话,我正在寻找更干净"的东西.

Can one bind a global flag to the "Enabled" property of the button, or is it more complicated than that? I know I could create a timed loop that checks for changes in Application.Documents, but I'm looking for something "cleaner" if possible.

我已经看过禁用自己的词2007 Add-In If No Document Loaded 和其他相关问题.

I've already looked at Disable Own Word 2007 Add-In If No Document Loaded and other related questions.

推荐答案

有几种方法可以解决这个问题.

There are several ways to handle this.

首先,您可以创建一个公开公开的函数,该函数为按钮的启用状态返回 true 或 false(无论您想确定它),然后定义您的功能区 xml 以指向该函数的 Enabled 属性 getter.如果您正在处理基于 IExtensibility 的插件,那么这就是您必须走的路.

first, you can create a publicly exposed function that returns true or false for the enabled state of your button (however you want to determine that), you then define your ribbon xml to point to that function for the Enabled property getter. If you're dealing with an IExtensibility based addin, then this is the way you'd have to go.

如果您正在处理 VSTO,则在功能区设计器中定义您的功能区按钮并使其默认禁用.

If you're dealing with VSTO, then define your ribbon button in the ribbon designer and make it DISABLED by default.

然后,在 STARTUP 事件期间,挂钩 WORD 对象,特别是 NEWDOCUMENT、DOCUMENTOPEN 和 WINDOWACTIVATE 事件.

Then, during the STARTUP event, hook the WORD object, specifically the NEWDOCUMENT, DOCUMENTOPEN and WINDOWACTIVATE events.

在每个事件的事件处理程序代码中,根据触发的事件和当时激活的文档启用或禁用您的按钮.

In the event handler code for each of those events, enable or disable your buttons as applicable depending on which event fired and which document was activated at the time.

这篇关于VSTO:仅在加载文档时启用功能区按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆