Invalidate ribbon control in Excel 2013 for multiple workbooks - vb.net

I have a customized ribbon where I added a tab containing a toggle button. This toggle button has a getPressed attribute which is linked to a callback function returning the pressed state of the toggle button. The purpose of the toggle button is to display/hide a custom task pane. This works fine.
However, my issue is that in Excel 2013, if I have two or more workbooks open, when I invalidate the toggle button, only the one of the active workbook is updated. I also want to update the pressed state of the toggle buttons on the other workbooks as the custom task pane is either visible or invisible in all workbooks.
Anyone knows how to do invalidate a control in the ribbon of all workbooks in Excel 2013?
I am using vb.net and excel-dna.
The toggle button is defined like that:
<toggleButton id="toggleButtonInputData" size="large" onAction="rxToggleButton_onAction" getPressed="rxToggleButton_getPressed" getImage="rxButton_GetImage" getLabel="rxbutton_GetLabel" getEnabled="rxGenericControl_GetEnabled" visible="true"/>
The callback function is:
Function rxToggleButton_GetPressed(ctl As CustomUI.IRibbonControl) As Object
Select Case ctl.Id
Case "toggleButtonInputData"
Return CTP_InputData.IsToggleButtonPressed
End Select
End Function
To invalidate the toggle button I use :
Public Sub CTP_InputData_VisibleStateChange() Handles CTP_InputData.VisibleStateChange
XLRibbon.myRibbon.InvalidateControl("toggleButtonInputData")
End Sub

The Invalidation is only processed for the active workbook's ribbon. But when you then switch to another workbook, the callbacks will fire again and will now be applied to the ribbon of the new workbook.
There's a bug and some quirks in Excel 2013 related to this switching:
If you click on the title bar or the ribbon of the workbook you want to activate, everything works as expected. But if you click on a cell in the workbook you're activating, you get two callbacks - the first is applied to the deactivating workbook's ribbon, and the second is applied to the activating workbook's ribbon. The problem is that you cannot distinguish (in your callback) whether you are getting called for the deactivating book. (Using COM events doesn't help either, both callbacks happen after all the COM Workbook- and Window- (De)Activate events have fired.
Apart from this quirky behaviour, one clear bug in Excel 2013 is that the IRibbonControl.Context is not set to the correct window - in both of the callbacks it reflects the activating window, though the first callback will be applied to the deactivating window.
Here's a detailed discussion on the issue: https://social.msdn.microsoft.com/Forums/windowsserver/en-US/a3dade87-1df7-46ec-8876-437194d7553e/how-to-reference-the-correct-workbook-from-a-control-in-a-ribbon-callback?forum=exceldev
In summary, you don't have good control over the state of deactivated ribbons. But if you are only worried about the active ribbon, Invalidate works fine, but you must expect the callback only upon activation.

Related

Under Excel O365, the custom ribbon tab does not always populate

Using an .xlam project with a fully working custom ribbon, I have found that the new tab will sometimes show nothing when clicked on. It is intermittent. The global RibbonUI object is available and running an .invalidate will then show the ribbon. However, the .invlalidate attempts prior to the tab being viewed do not seem to change this behavior. I have to view the tab and then run the .invalidate manually (with a macro button) to see the ribbon appear.
I am scratching my head trying to find a slick way of making this new custom tab populate 100% of the time.
For the benefit of those that come across this issue, I have found the root cause and a solution. This is Excel O365 specific.
If you have a custom ribbon tab set up and working, try this. Open Excel, create a blank workbook, then double-click on a cell to put it in "edit mode". Click on your custom ribbon tab. Blank, right?
This "cell in edit mode" is what was causing Excel O365 to fail to show my custom ribbon. Excel O365 starts up on the "File" screen. Prior versions always opened to a blank sheet. If you click on "Blank workbook", the ribbon displays fine. If you DOUBLE-CLICK on "Blank workbook", the first click opens the workbook and the second click puts a cell into ""edit mode". In this state, clicking on your custom ribbon tab shows nothing.
The work-around
Private Sub WorkbookActivate(ByVal Wb As Workbook)
Application.SendKeys "{ESC}"
...
You may see a cell go into edit mode for one blink but it then exits edit mode. Click on your custom ribbon tab. It will now show the ribbon controls as designed.

Refreshing Custom Ribbon Tab in Excel 2010 When Someone Clicks on the Tab

I've been writing a custom ribbon with VSTO (using VB.net and XML) and I have written a number of buttons who's labels & pressed states depend on the calculation mode that Excel is in.
The getPressed and getLabels callback functions check for the excel calculation mode but these are only called when the ribbon is first loaded or the buttons are clicked. If someone were to go into the "formulas" tab, change the calculation mode and then go back to the custom ribbon tab, the pressed states and labels on the buttons in this custom tab would now be incorrect.
Is there a way to trigger these callback functions to be called (eg a way to call the ribbon.invalidate() function) when someone clicks on the custom ribbon tab in Excel?

Excel AddIn - Keeping windows form always visible while w/in Excel

First of all, thank you for your time and assistance in reviewing this!...
I'm trying to upgrade an Excel VBA workbook to a VSTO Excel Add-in in VB.NET using VS 2010. In the original (i.e.- VBA) version I have a modeless UserForm (called frmMain) that floats on top and is visible at all times while the user is still within the Excel application, but is not visible if the user moves to another window outside of Excel.
For example, within Excel the user can click on any worksheet tab, select any cell, etc. and the UserForm is still visible. This is exactly how I'd like it.
The problem is, that in the new VSTO add-in, I can not get the Windows form to mimic this same behavior.
I use frmMain.Show() to show the form as a modeless form, but the moment the user clicks an Excel worksheet (i.e.- activates a worksheet) the form becomes hidden behind the worksheet.
I can manually Alt-Tab to bring the form back into view, but I need it to always remain in view - floating on top of the Excel worksheets so long as the user hasn't left the Excel application.
I tried various things, including setting the form to TopMost, however, that causes the form to be TopMost everywhere - including outside of Excel. Worse than that, if the user does anything that would normally result in Excel's launching a dialog box (e.g.- closing an open workbook, raising the alert "Do you want to save the changes...") the alert dialog box itself is hidden and inaccessible behind the frmMain form (since the frmMain is TopMost).
How can I get my form to behave in the desired way (i.e.- the same way it did in VBA)?
Thanks!!!
Rob
You should take a look at Custom Task Panes which can be docked or floated within the Excel application. You could also look into the COM interfaces for a lower level connection (see related SO Post) - although Task Panes are really what this type of behavior was intended for.
This method might work (worked for me):
Create a worksheet_selectionChange event handler inside your form class
Put these lines in this event handler:
Dim FormHandle As IntPtr = Me.Handle
Me.Visible = False
Me.Show(NativeWindow.FromHandle(FormHandle))

Refreshing the ribbon interface after a call from a macro in an excel sheet

Here is my scenario:
I have an old excel project with macros that I've imported in a vsto project.
I have designed a custom ribbon (startFromScratch = true) to make it look like a dictator application.
I still want to use the existing macro code since it would take too much time to translate all the vba code in c# code
I have a class that is marked with the [ComVisible(true)] attribute so that I can call vsto methods from the vba code.
The problem is that I can't hide tabs, I can't get the ribbon to refresh. I can change the state of other controls (e.g. set checked state for CheckBox), but I can't hide or disable my custom tabs.
I have tried PerformLayout(), PerformDynamicLayout() on the ribbon, ribbon.RibbonUI.Invalidate(), but nothing works, the tab never changes state, although in code I set its visible property to false.
What do I need to do in order to refresh the ribbon at runtime?
Edit:
I just tried the same thing by exporting the ribbon to xml and the Invalidate() method works as expected. Is there any way to accomplish this for a ribbon designed with the visual editor?
I've replicated your problem when I set the StartFromScratch property of the ribbon to True, but it goes away when I set it to False. This is confirmed by MSDN.
You cannot change the visibility of custom tabs at run time if this property is set to true.
As a workaround, set StartFromScratch to False, add in a tab for each default tab, setting the ControlIdType of each to Office and the OfficeId of each to their default name (TabHome, TabFormulas, etc.), then set each of these built-in tab's visibility to False. (MSDN has made available a full list of control IDs for their Office apps for easy reference.)

In Excel, is there a way to determine the method being called by a password protected 3rd-party ribbon button?

I'm looking to automate a simple process in VBA and need to "click" a ribbon button. It makes the most sense to just call the button's underlying method. Is there a way to figure out what it is?
We are using Excel 2007 and 2010.
If it's Excel 2003 or earlier, you can use the CommandbarControl.Execute method. For instance:
Application.Commandbars("3rd Party Toolbar").Controls("Button to Push").Execute
There are two ways you can do this.
Right click on the toolbar and select Customise. If you right click on the button there may be details somewhere of what Macro the button is associated with.
If you record a macro and click the button. Then you can look at the VBA workspace and view the code that is generated to run your macro (which is just clicking the button you want). This will show you what method the macro clicks.