Reputation: 4044
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
Upvotes: 0
Views: 4417
Reputation: 4489
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.
Upvotes: 3
Reputation: 3298
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
}
Upvotes: 1