Is there a way to access a Ribbon (XML) at run time?
I'm working on a Word 2007 app-level add-in. So far, I haven't experienced ma开发者_StackOverflow社区jor obstacles except for converting the add-in's Ribbon interface to XML. I have to use Ribbon XML because the feature I'm working on can only be done this way. The problem is that by switching to Ribbon XML I can no longer access the interface at run time via Globals.Ribbons. This link http://msdn.microsoft.com/en-us/library/bb772088.aspx does a good job explaining how to access a Visual Designer Ribbon but it completely ignores the XML Ribbon case. Specifically, I need to be able to access some visual controls such as labels. How can I achieve this?
Globals.Ribbons is a VSTO designer feature, if you use RibbonXML then you don't have this feature. What the designer actually does under the covers is it will create ribbon xml for Office, then when office makes a callback, VSTO will raise the appropriate event handler for that context (document). Because you are using RibbonXML you are bypassing the VSTO Ribbon designer support entirely (I prefer it this way, it is faster and you have more control).
With ribbon XML you will have to register a onLoad callback for your label, Office will then pass you a IRibbonControl, which will be the label, and you have limited stuff you can do. If you wanted to change the text then off the top of my head you would have to register a getText callback, then invalidate that ribbon control, which will cause the getText callback to be reevaluated.
Having more info about what you actually want to achieve would be handy =) I have a feeling my VSTO contrib project will also make your life much easier, as it gives you many of the nice Ribbon Designer features when using ribbon xml. But let me know what it is you want to do, and I can give you more info about that.
Cheers, Jake
When working with Ribbon XML, I tried this but I couldn't access the Ribbon1 property from the Globals.Ribbons.. The property simple wasn't there..
However, I came up with another solution which basically had to do with a proper type cast.
In ThisAddIn.cs:
private Microsoft.Office.Core.IRibbonExtensibility ribbonObj;
protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
ribbonObj = new Ribbon1(this);
return ribbonObj;
}
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
// Calling the public method TEST() in Ribbon1.cs
//MyNameSpace is the namespace used in your project ie., your project name
((MyNameSpace.Ribbon1)ribbonObj).TEST();
// Calling the public variable flag in Ribbon1.cs
((MyNameSpace.Ribbon1)ribbonObj).flag;
}
It depends on when you are trying to access Globals.ribbons.
As I recall, it won't be populated until very near the end of the start up phase of Word.
if you try and access it too early, there won't be any ribbons defined yet.
精彩评论