VSTO:为什么 OfficeRibbon.Context 为空?

发布于 2024-07-07 20:27:28 字数 1116 浏览 4 评论 0原文

我正在开发我的第一个 Word 2007 插件,并且我已将 OfficeRibbon 添加到我的项目中。 在按钮单击处理程序中,我想要对当前 Word.DocumentWord.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.

  • Here's the online help for this property;

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

擦肩而过的背影 2024-07-14 20:27:28

我在使用 VS2008 SP1 创建 Excel 2007 AddIn 时也遇到了这个问题。 我使用的解决方法是将应用程序存储在主 AddIn 类的内部静态属性中,然后在功能区的事件处理程序中引用它:

public partial class ThisAddIn
{
    internal static Application Context { get; private set; }

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        Context = Application;
    }
    ...
}

public partial class MyRibbon : OfficeRibbon
{
    private void button1_Click(object sender, RibbonControlEventArgs e)
    {
        DoStuffWithApplication(ThisAddIn.Context);
    }
    ...
}

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:

public partial class ThisAddIn
{
    internal static Application Context { get; private set; }

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        Context = Application;
    }
    ...
}

public partial class MyRibbon : OfficeRibbon
{
    private void button1_Click(object sender, RibbonControlEventArgs e)
    {
        DoStuffWithApplication(ThisAddIn.Context);
    }
    ...
}
后知后觉 2024-07-14 20:27:28

尝试使用以下内容引用该文档:

Globals.ThisDocument.[some item]

MSDN 参考

Try referencing the document with:

Globals.ThisDocument.[some item]

MSDN Reference

妖妓 2024-07-14 20:27:28

从以下位置获取:

Globals.ThisAddIn.Application

Get it from:

Globals.ThisAddIn.Application

入怼 2024-07-14 20:27:28

虽然我不太了解 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文