I'm trying to create a simple Word add-in. I the Ribbon a have create a button and I want to change its label depending on the type of word document. the document is a mail merge that has two types of models: Demand and Response. I want to have button label: CREATE DEMAND for Demmand and CREATE RESPONSE for Response.
I tried something like this, but it does not work:
private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
{
Microsoft.Office.Interop.Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
//Word.MailMerge mailMerge = doc.MailMerge;
if (doc.MailMerge.DataSource.FieldNames.Count!=0)
{
foreach (Microsoft.Office.Interop.Word.MailMergeFieldName f in doc.MailMerge.DataSource.FieldNames)
{
if (f.Name.Equals("Response"))
{
this.button1.Label = "Create a response";
break;
}
else if (f.Name.Equals("Demand"))
{
this.button1.Label = "Create a demand";
break;
}
}
}
}
the value of doc.MailMerge.DataSource.FieldNames.Count is always equal to 0 Do you have any idea how i can do it?
The visual designer for ribbons in word is a little limited, you can get much more functionality by using an XML ribbon, although it is a little more work.
You could do handle this by creating two buttons and then creating a
GetVisible
method in your ribbon code. The XML would be the following:The code would be something like this
Alternatively you could use a
GetLabel()
method to return the label for the button.You add the DocumentChange() event to the ThisAddIn class