VSTO:为什么 OfficeRibbon.Context 为空?
我正在开发我的第一个 Word 2007 插件,并且我已将 OfficeRibbon 添加到我的项目中。 在按钮单击处理程序中,我想要对当前 Word.Document
或 Word.Application
的引用。
我试图通过 OfficeRibbon.Context
属性获取引用,文档称该属性应引用当前的 Application
对象。 但是,它始终为 null
。
有谁知道
a) 是否需要做一些事情才能使 OfficeRibbon.Context
显示正确填充?
b) 是否有其他方法可以获取 Word 应用程序或活动 Word 文档的参考?
注意:
我正在使用 VS2008 SP1
功能区看起来已初始化良好:功能区在 Word 中正确呈现; 我可以单步执行构造函数和 OnLoad 成员; 按钮单击处理程序正确执行。
这里是该属性的在线帮助;
OfficeRibbon.Context 属性
C#
公共对象上下文{ get; 内部设置; }
表示与此 OfficeRibbon 对象关联的检查器窗口或应用程序实例的对象。
备注
在 Outlook 中,此属性指的是显示此 OfficeRibbon 的检查器窗口。
在 Excel、Word 和 PowerPoint 中,此属性返回显示此 OfficeRibbon 的应用程序实例。
I'm developing my first Word 2007 addin, and I've added an OfficeRibbon to my project. In a button-click handler, I'd like a reference to either the current Word.Document
or Word.Application
.
I'm trying to get a reference via the OfficeRibbon.Context
property, which the documentation says should refer to the current Application
object. However, it is always null
.
Does anyone know either
a) if there is something I need to do to make OfficeRibbon.Context
appear correctly populated?
b) if there is some other way I can get a reference to the Word Application or active Word Document?
Notes:
I'm using VS2008 SP1
The ribbon looks like it has initialized fine: The ribbon renders correctly in Word; I can step the debugger through both the constructor and the OnLoad members; Button click handlers execute correctly.
OfficeRibbon.Context Property
C#
public Object Context { get; internal set; }
An Object that represents the Inspector window or application instance that is associated with this OfficeRibbon object.
Remarks
In Outlook, this property refers to the Inspector window in which this OfficeRibbon is displayed.
In Excel, Word, and PowerPoint, this property returns the application instance in which this OfficeRibbon is displayed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我在使用 VS2008 SP1 创建 Excel 2007 AddIn 时也遇到了这个问题。 我使用的解决方法是将应用程序存储在主 AddIn 类的内部静态属性中,然后在功能区的事件处理程序中引用它:
I also encountered this problem while creating an Excel 2007 AddIn using VS2008 SP1. The workaround I used was to store the Application in an
internal static
property in the main AddIn class and then reference it in the event handler in my ribbon:尝试使用以下内容引用该文档:
MSDN 参考
Try referencing the document with:
MSDN Reference
从以下位置获取:
Globals.ThisAddIn.Application
Get it from:
Globals.ThisAddIn.Application
虽然我不太了解 Office 2007 Word 对象模型的变化,但这里是我使用 VBA 知识的解释。
应用程序是一个全局可用的对象。
另外,Application.ActiveDocument 应该让您处理当前文档。
推测:您打算如何添加功能区?
While I dont know much about changes in Office 2007 word object model, here is my explanation using VBA knowledge.
Application is a globally available object.
Also, Application.ActiveDocument should get you handle to the current document.
Speculating: How are you trying to add the ribbon?