VSTO: Enable ribbon button only when a document is loaded - c#

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.

Related

Change office ribbon run time

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).

Hide context tabs excel from ribbon designer c#

I've been crashing my head over this problem for far too long... but I can't find any confortable solution.
I need to hide contextual tabs (i.e. TabTableToolsDesignExcel) from my custom ribbon. For normal tabs it is sufficient to create a tab with the same idMso from the designer but, with contextual tabs, this doesn't work. So anytime a user enter a table over a worksheet the contextual menu is shown.
Although I know it would work exporting to xml and creating all callbacks manually I'd really appreciate to avoid this way cos it requires a lot of effort...
Is there any trick to hide a contextual tabset or, even, to catch the context change event?
Thx in advance for your help.
You can create a tab with the same IdMso value, but! instead of declaring the visible property use the getVisible callback. It is possible only with the custom UI XML markup, not designer. The callback will be called when the tab is going to be shown. In the handler you can return false to hide the tab. Hope it helps.
You can read more about the Ribbon UI in the Customizing the 2007 Office Fluent Ribbon for Developers article in MSDN.

How to navigate programmatically in an Office 2010 addin?

Does anybody know if it is possible (and in that case how) to navigate programmatically in an Office 2010 addin?
The idea is to create a settings button on a ribbon tab, that when clicked, will direct the user to the settings of the add-in that would be located in the backstage view. Is there a way to programmatically change the active view being displayed to the user or something like that?
Thank you!!
As a developer putting on the user spectacles, I find your approach not convincing. If I click on a settings button, I expect a well-designed dialog, not to be thrown into backstage view. On the other hand - as a developr I'm much more at ease with a well-designed Windows Form than with the controls Microsoft provides for the backstage user interface; I guess you'll find there more limitations then you like.
As far as I understand your question, you want to have two different entry points to your settings dialogue - one from a button in the ribbon, and another from a point in the backstage view. Why not combining and showing from both positions the same form? Also Microsoft provides you with additional dialogues, if you click on controls in the backstage view.
Technically:
If you want to start the action of a ribbon control, you can use the "ExecuteMSO" command of the application.commmandbar object, e.g. in Word you may use
Application.CommandBars.ExecuteMso "ApplicationOptionsDialog"
to open this dialogue. However, I've done a limited test to call a custom button in backstage view, and it failed. Sol I guess that you can use ExecuteMSO only for built-in commands.

How do I customize Word 2010 ribbon only on demand in a VSTO4 add-in?

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 =)

Add Excel ribbon controls at runtime (VSTO 2005SE)

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?

Categories