change the Label of a button in ribbon word add in

597 views Asked by At

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?

1

There are 1 answers

3
Charlie On BEST ANSWER

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:

<button id="ButtonCreateDemand" label="Create Demand"     size="normal"getVisible="GetVisible" onAction="Call_CreateDemand"/>
<button id="ButtonCreateResponse" label="Create Response" size="normal"getVisible="GetVisible" onAction="Call_CreateResponse"/>

The code would be something like this

public bool GetVisible(Office.IRibbonControl control)
{
    //Check for demand\response

    switch(control.Id.ToLower())
    {
       case "buttoncreatedemand":
       return GetDemand();
       case "buttoncreateresponse":
       return !GetDemand();           
    }

}

private bool GetDemand()
{
     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"))
                {
                    return false;
                }
            }
        }
        return true;
}

Alternatively you could use a GetLabel() method to return the label for the button.

You add the DocumentChange() event to the ThisAddIn class

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        Globals.ThisAddIn.Application.DocumentChange += new Word.ApplicationEvents4_DocumentChangeEventHandler(DocumentChange);
    }