none
Enable ribbon controls in specific documents C# vsto 2010 RRS feed

  • Question

  • I have an MS Office 2010 application level add-in, when it's loaded all ribbon controls in my custom tab are disabled.Then based on certain conditions I run this method to enable all the ribbon controls in my custom tab:

    public void EnableRibbonControls()
            {
                IUnityContainer container = ServiceLocator.Current.GetInstance<IUnityContainer>();
                RibbonTab customTab = container.Resolve<RibbonTab>();
    
                for (int i = 0; i < customTab.Groups.Count; i++)
                {
                    IList<RibbonControl> controls = customTab.Groups[i].Items;
                    foreach (var control in controls)
                    {
                        control.Enabled = true;
                    }
                }
            }

    The problem is that this code enables the ribbon controls in the ribbon of every open Word document and not the specific one that I'm working on. I would like to know whether the only way to fix this is by implementing a document level add-in or does someone know a work around for this in the application level add-in?
    Friday, July 20, 2012 8:08 AM

Answers

  • Hi Denys

    You could use the DocumentChange event to trigger this method. When the event is triggered, Application.ActiveDocument will be the document getting the focus. Based on some criterium, your code would enable/disable the controls as required.


    Cindy Meister, VSTO/Word MVP

    • Marked as answer by Denys W Monday, July 23, 2012 9:24 AM
    Friday, July 20, 2012 12:55 PM
    Moderator

All replies

  • you are calling Enabled unconditionally so no suprise there. I would use callbacks for getEnabled. Those callbacks will get context as a parameter and that context should be used to determine if controls should be enabled - this way you will not enable buttons everywhere.

    Friday, July 20, 2012 8:58 AM
  • Hello Damian

    Could you please provide an example or a helpfull URL?

    Friday, July 20, 2012 11:19 AM
  • Hi Denys

    You could use the DocumentChange event to trigger this method. When the event is triggered, Application.ActiveDocument will be the document getting the focus. Based on some criterium, your code would enable/disable the controls as required.


    Cindy Meister, VSTO/Word MVP

    • Marked as answer by Denys W Monday, July 23, 2012 9:24 AM
    Friday, July 20, 2012 12:55 PM
    Moderator
  • Thank you Cindy

    Your approach is working, just need to see what the cost of implementing it this way will be on performance,

    because the method used to toggle the Enabled of status of the buttons is executed every time the document changes.

    Can you think of any other ways to implement this or is the method you've suggested and the Document-Level add ins the only two options?

    Monday, July 23, 2012 9:30 AM
  • Hi Denys

    If you want this to be an application-level add-in, then you really don't have much choice as far as the event goes. The only thing I can suggest is that you test something (which document it is) before invalidating the Ribbon, so that changing the status of the buttons only triggers when absolutely necessary.

    The only other thing that occurs to me would be that you could supply a Word template as part of the project and link the documents to that. Such a template can provide a Ribbon that will be specific to the document BUT it would also have to carry VBA code to provide functionality for the RIbbon controls.


    Cindy Meister, VSTO/Word MVP

    Monday, July 23, 2012 9:53 AM
    Moderator