views:

585

answers:

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.

+1  A: 

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?

shahkalpesh
+2  A: 

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);
    }
    ...
}
Joseph Sturtevant
+1  A: 

Try referencing the document with:

Globals.ThisDocument.[some item]

MSDN Reference

Mike Regan