0
votes

I'm trying to create a simple Word add-in. I have created a Word 2010 add-in project with this auto-generated code:

public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject() {
            OwnRibbon ribbon = new OwnRibbon();
            //ribbon.DocumentProperty = //get the document here
            return ribbon;
        }
        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }

I've looked at the documentation and I understand how I can add text to the document from this class. What I have however, is a Ribbon (created via new item -> ribbon (visual designer)) with two buttons.

When a button is pressed, I would like to add text to the document. However, this ribbon creates a seperate class:

public partial class OwnRibbon
    {
        private void OwnRibbon_Load(object sender, RibbonUIEventArgs e)
        {

        }

        private void btnInvoegen_Click(object sender, RibbonControlEventArgs e)
        {
        }
    }

How can I access the document from the click event handler?

Thanks

2

2 Answers

3
votes

Try this piece of code

Microsoft.Office.Tools.Word.Document vstoDocument =
 Globals.ThisAddIn.Application.ActiveDocument.GetVstoObject();

Lots of Office objects are accessible through static methods like this.

1
votes

You can create a property in your class and than set it in ThisAddIn class:

protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject() {
            OwnRibbon ribbon = new OwnRibbon();
            ribbon.DocumentProperty = //get the document here
            return ribbon;
        }

In the OwnRibbon class:

private void btnInvoegen_Click(object sender, RibbonControlEventArgs e)
{
    //use DocumentProperty which holds the document
}