joeschwa
joeschwa

Reputation: 3175

Expose VSTO Ribbon to VisibleChanged event in C#

I am creating an application level add-in for Word 2010 using C# VSTO. I want to access the get_Pressed callback method for the add-in's ribbon when the add-in's Custom Task Pane is hidden or made visible. However, in order to do so, I need the ribbon to be made available to the myTaskPane_VisibleChanged event in the ThisAddIn class. I cannot use the Ribbons collection because the ribbon in the add-in was created in Ribbon XML and not with Visual Studio's Ribbon Designer.

In the ThisAddIn class I have:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    myTaskPane.VisibleChanged += new EventHandler(myTaskPane_VisibleChanged);
}

and

protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
    return new myRibbon();
    //I tried playing with IRibbonExtension here, but could not get that to work
}

and

public void myTaskPane_VisibleChanged(object sender, System.EventArgs e)
{
    //Here is where I would like to access the ribbon
    //I think the command would look something like:
    //myRibbon.IsTaskPaneVisible = !myRibbon.IsTaskPaneVisible;
   //myRibbon is not accessible here
}

In the myRibbon class I have:

public class myRibbon : Office.IRibbonExtensibility
{
    public Office.IRibbonUI ribbon;

    private bool isTaskPaneVisible;

    public bool IsTaskPaneVisible
    {
        get { return isTaskPaneVisible; }
        set
        {
            isTaskPaneVisible = value;
            ribbon.InvalidateControl("rxtglElementsPane");
        }
    }

and

public bool rxtglElementsPane_get_Pressed(Office.IRibbonControl control)
{
    try
    {
        switch (control.Id)
        {
            case "rxtglElementsPane":
                return isTaskPaneVisible;
            default:
                return false;
        }       
        catch
        {
            return false;
        }
    }
}

Most of this code is based on this article:

Synchronizing Ribbon and Task Pane

where in the comments the author mentions that generated code in the CreateRibbonExtensibilityObject includes the instantiation of the ribbon. When I created the add-in there was no such code generated by Visual Studio 2013.

Any help in accessing the ribbon from the ThisAddIn class would be greatly appreciated.

Upvotes: 1

Views: 946

Answers (2)

joeschwa
joeschwa

Reputation: 3175

Starain Chen of the MSDN Visual Studio forum provided me with the answer from a post I placed there:

MSDN Expose VSTO Ribbon to VisibleChanged event in C# Answer

The solution was to define a field/property to the ThisAddIn class with the myRibbon type.

public partial class ThisAddIn
{
   internal myRibbon myRibbon;
   ...
    protected override Office.IRibbonExtensibility CreateRibbonExtensibilityObject()
    {
        myRibbon=new myRibbon();
        return myRibbon;
    }
}

Upvotes: 2

Eugene Astafiev
Eugene Astafiev

Reputation: 49395

There is no need to access the ribbon objects. Instead, you may access the add-in class using the Globals.ThisAddin property. See Global Access to Objects in Office Projects for more information. Thus, you will be able to modify any local variables that can be used in the Ribbon callbacks.

You can read more about the Fluent UI (aka Ribbon UI) in the following series of articles in MSDN:

Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3)

Customizing the 2007 Office Fluent Ribbon for Developers (Part 2 of 3)

Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)

Upvotes: 0

Related Questions