3

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.

4

4 に答える 4

4

VS2008SP1を使用してExcel2007アドインを作成しているときにも、この問題が発生しました。私が使用した回避策は、メインのAddInクラスのプロパティにアプリケーションを格納internal staticし、リボンのイベントハンドラーでそれを参照することでした。

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);
    }
    ...
}
于 2009-05-21T22:05:39.340 に答える
2

入手先:

Globals.ThisAddIn.Application

于 2012-06-13T21:21:49.690 に答える
2

次の方法でドキュメントを参照してみてください。

Globals.ThisDocument.[some item]

MSDN リファレンス

于 2009-05-22T06:41:14.480 に答える
1

Office 2007 のワード オブジェクト モデルの変更についてはよくわかりませんが、VBA の知識を使用して説明します。

アプリケーションはグローバルに利用可能なオブジェクトです。また、Application.ActiveDocument は、現在のドキュメントへのハンドルを取得する必要があります。

憶測: どのようにリボンを追加しようとしていますか?

于 2008-11-02T04:32:51.687 に答える