Search code examples
c#classvstoribbon

VSTO : how to set a ribbon's button from ThisAddIn class


I have a ribbon, that has button in it. When a user selects new document, I would like to change the ribbon's button's text. I think I've done everything but one line:

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        var wb = this.Application;
        ((Word.ApplicationEvents4_Event)wb).NewDocument += new Word.ApplicationEvents4_NewDocumentEventHandler(Application_NewDocument);

    }
    /// <summary>
    /// When the new button is pressed, the save should be enabled
    /// </summary>
    /// <param name="Doc"></param>
    void Application_NewDocument(Microsoft.Office.Interop.Word.Document Doc)
    {
    // set button's properties here
    }

Solution

  • The most easy way is to put the event handler in the Ribbon class. Then you can simply use:

    this.Button.Label = "text";
    

    Else, you have to create a property marshaling it to the outside world:

    In Ribbon:

    public string ButtonText {get {return this.Button.Label;} set {this.Button.Label = value;} }
    

    In ThisAddIn:

    Globals.Ribbons.Ribbon.ButtonText = "text";