This seems to have been asked several times on here with no real answer:
Accessing Ribbon Controls Programatically in an XML Ribbon
Office Ribbon: How to access a control when the ribbon was created using XML
If a ribbon is created using ribbon xml, as far as I can tell, there is no programmatic exposure of the components defined in that xml. The only obvious limited way to change the state of these components is to use something like an onAction, getContent, getImage event, and the IRibbonControl element offers very little for manipulating the element in question (not to mention, most of those functions only run once, when first rendered).
So I'm trying to find a way to programmatically access those xml ribbon elements.
There is no way to programmatically access Ribbon elements when using Ribbon XML. If you want to change the state of the Ribbon controls - you need to use IRibbonUI.Invalidate() to force a new rendering of the layout (potentially using properties that trigger different behaviors at rendering time via callbacks). This can be a good thing as you have more control over when drawing occurs if you are changing multiple items' state.
See related SO post on updating Ribbon UI control state.
If you want programmatic access to Ribbon elements, you should use the Ribbon Designer. However, as stated on MSDN, the designer doesn't support all customizations. Some speculate that the designer just wraps the Ribbon XML up for you under the hood.
You just have to learn the callback mechanism utilized by the Ribbon XML - there is no control tree for you to gain access to.
Related
I have a word add on project with two ribbon.xml files. What I need to do is change the current ribbon at run time.
the method DocumentBase.CreateRibbonExtensibilityObject() can use to do the job. But I cannot find way to trigger this method at run time.
when app starts this method triggers.
protected override Microsoft.Office.Core.IRibbonExtensibility reateRibbonExtensibilityObject()
{
if (condition == true)
{
return Ribbon1;
}
else
{
return Ribbon2;
}
}
I need to change the current ribbon of application at run time
This behavior is by design. The Ribbon XML used must be pre-defined when a project is loaded; it cannot be changed or created at run-time.
It's possible to dynamically display or enable controls using call-backs. Labels and images on controls can also be loaded dynamically. Lists and dynamic menus can be populated dynamically. Put all you could ever need in the Ribbon XML at design time then hide/disable what should not be available by default.
(Responding to follow-up in comments) Also by design, it's not possible to work with third-party Ribbon customizations. Too many developers were manipulating both built-in and third-party menus and toolbars - users thought the Office application was "buggy" because things were disappearing/changing. So the Ribbon UI only allows accessing third-party controls if those have been purposely "shared" (by using the idQ attribute).
I am trying to rename the backstage view control(File -> Info -> Protect Document -> restrict permission by people) for word 2010.
I thought of two solutions.
In XML if I can get the idmso control, I could change label or get label. But I am not able to do so as in XML I am not getting control inside group node.
If findcontrol method of C# could help me to find the back stage view like Other ribbon then I might set the label.
But all the above are not fruitfull for me.
even I don't know its possible or not. I am new to VSTO development so might my solution sound funny.
But I am not able to do so as in XML I am not getting control inside group node.
What exactly do you need to do with the Backstage UI?
Note, it is not possible to override these commands from the custom UI XML markup or by using the object model. A possible solution would be to consider hiding the built-in UI and rebuilding it fully with custom commands.
You can read more about the Backstage UI in the following articles in MSDN:
Introduction to the Office 2010 Backstage View for Developers
Customizing the Office 2010 Backstage View for Developers
How does one set a ribbon button in a Word add-in to be enabled when a document is loaded and disabled when no documents are loaded, just Like most of the built-in buttons?
Can one bind a global flag to the "Enabled" property of the button, or is it more complicated than that? I know I could create a timed loop that checks for changes in Application.Documents, but I'm looking for something "cleaner" if possible.
I've already looked at Disable Own Word 2007 Add-In If No Document Loaded and other related questions.
There are several ways to handle this.
first, you can create a publicly exposed function that returns true or false for the enabled state of your button (however you want to determine that), you then define your ribbon xml to point to that function for the Enabled property getter. If you're dealing with an IExtensibility based addin, then this is the way you'd have to go.
If you're dealing with VSTO, then define your ribbon button in the ribbon designer and make it DISABLED by default.
Then, during the STARTUP event, hook the WORD object, specifically the NEWDOCUMENT, DOCUMENTOPEN and WINDOWACTIVATE events.
In the event handler code for each of those events, enable or disable your buttons as applicable depending on which event fired and which document was activated at the time.
Use the DocumentChange event instead. Hook up will be something like this:
Globals.ThisAddIn.Application.DocumentChange += new EventHandler(OnDocumentChange);
And the Handler
void OnDocumentChange()
{
this.myButton.Enabled = wordApp.Documents.Count > 0;
}
Interesting, my VSTO Contrib project (http://vstocontrib.codeplex.com/documentation) has some features which make ribbon management simpler.
The cleanest way is to use my ribbon factory, but the project will need to be updated to disable buttons if there are no viewmodels to query for the status of the button. In fact it is a scenario I havent really covered.
You have 3 parts an add-in is interested in, the view (window), the context (the document) and the ribbon. VSTO Contrib means you get a view model per context, and it manages/abstracts the ribbon and view so it appears you have a ribbon per context, and it tells you the current active view (for multiple windows showing same document scenarios).
The missing part is if there is a ribbon, but no contexts and no viewmodels, it should invalidate that ribbon control and disable it. It should be a pretty simple change, email me if you are interested in giving VSTO Contrib's RibbonFactory a spin and I can make this change for you.
I am currently diving into VSTO add-in development and ribbon customization for Word 2010. This works fine, but has a drawback: The ribbon customization should take place for only certain documents, but the IRibbonExtensibility.GetCustomUI() method is obviously called on application startup before any document is loaded. How can I accomplish this requirement?
You're right that it's only called once. About your only choice (and what's worked for me reasonably well), is to define EVERYTHING in your ribbon up front, then set the VISIBLE property to false or true as applicable depending on what doc is loaded, or whatever else is happening in your addin.
#drventure is right, you have to define your ribbon up front, then show/hide different buttons on the ribbon.
What might help you though is some of the features in a project I have been working on, VSTO Contrib, it allows you to create a ViewModel, which will be created for each open document, and it will discover and wire up the ribbon xml by convention.
In your case this would mean you can bind the ribbon buttons visibility to a property on the viewmodel, and you don't have to worry about keeping the window/documents syncronised. Have a read of http://jake.ginnivan.net/vsto-contrib/ribbon-factory and see if it will help you solve your problem with a heap less code =)
Does anyone know how to modify the content of the Excel ribbon at runtime with VSTO 2005SE? Not only update labels or dynamic menus, but also add or remove buttons, tabs, groups, drop downs etc. At runtime means not only at my add-in startup, but also during work with Excel.
Irrespective of VS version, I don't think all that you want is actually possible with the current version of the RibbonX control*. Specifically, there's no functionality for adding and removing. You can control visibility, though, so you can put everything in by default and make it visible or otherwise as needed.
* which was 2007 at time of writing - Excel/Office 2010 may have changed things
I agree with Mike, working with the visibility callback on controls or groups is probably your best bet (that's what we are using).
The entire ribbon layout is loaded from an XML string. I don't know if it is possible to trigger a reload of the XML, which you could then customize to load different XML content.
If you use Visual Studio 2008, you have a great ribbon designer.
Do you want to have methods that is triggered by excel?