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

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

Related

Word VSTO - Document Changed Event

we are using the Application.DocumentChange event so that when a document is loaded, it can check the name of the document, and then if it is named in a certain way show or hide buttons on the ribbon.
If I use the code below it works really well, it shows or hides the buttons correctly.
But when I run the addin in debug mode from Visual Studio, when the document is closing it gives this message.
System.Runtime.InteropServices.COMException: 'This command is not available because no document is open.'
As you can see I tried to put in an if statement to stop it running the code but it didnt work.
If I just install my addin, Word does not seem to crash or have any problems, maybe I shouldnt worry about it?
Thanks
Private Sub Application_DocumentChange() Handles Application.DocumentChange
If Globals.ThisAddIn.Application.Documents.Count > 0 Then
If Globals.ThisAddIn.Application.ActiveDocument.BuiltInDocumentProperties("Title").Value = "Document Name Here" Then
Globals.Ribbons.VisualfilesTab.AttachEvidence.Visible = True
Else
Globals.Ribbons.VisualfilesTab.GetAuthorisation.Visible = True
End If
End If
End Sub
Consider switching your custom UI to the ribbon XML over the designer-generated custom ribbon UI. You can export your existing custom UI to the ribbon XML markup. See How to: Export a ribbon from the Ribbon Designer to Ribbon XML for more information.
You can customize the Ribbon UI by using callback procedures in COM add-ins. For each of the callbacks that the add-in implements, the responses are cached.
For example, if an add-in writer implements the getVisible callback procedure for a button, the function is called once, the state is loaded, and then if the visibility state needs to be updated, the cached state is used instead of recalling the procedure. This process remains in place until the add-in signals that the cached values are invalid by using the Invalidate or IRibbonUI.InvalidateControl method, at which time, the callback procedure is again called and the return response is cached. The add-in can then force an immediate update of the UI by calling the Refresh method.
Read more about the the Fluent UI (aka Ribbon UI) in the following articles:
Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 2 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)

synchronize Outlook Ribbon togglebutton with registry settings in VB.Net for an Add-In

I am building an Outlook Add-In in Visual Basic and I have a togglebutton in the ribbon that controls whether a setting is on or off. This settings is saved and retrieved using "GetSettings" and "SaveSetting" which saves information to the Windows Registry (to my understanding). I want the togglebutton the correspond to changes made to that setting as it can be changed in other places than just the togglebutton. My problem would be solved if I could access the value of the togglebutton and setting it manually but as I have understood it: I cannot access it as I am building my ribbon in XML. I have not been able to find a solution to this, can anyone help?
Side note: If I add my togglebutton to the taskbar in Outlook and from there toggle the settings, the togglebutton in the ribbon doesn't change to "pressed" mode. Perhaps this is something that can be fixed with any provided solution from anyone :)
You need to use IRibbonUI.Invalidate or for a single control you may use the IRibbonUI.InvalidateControl method to get your ribbon callbacks invoked for updating the cached values according to the state loaded from settings.
For example, if an add-in implements the getPressed callback procedure for a toggle button, the function is called once, the state loads, and then if the state needs to be updated, the cached state is used instead of recalling the procedure. This process remains in place until the add-in signals that the cached values are invalid by using the Invalidate method, at which time, the callback procedure is again called and the return response is cached. The add-in can then force an immediate update of the UI by calling the Refresh method (VSTO).
You can read more about the Fluent UI (aka Ribbon UI) in the following articles:
Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 2 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)
Do not rely on the button to store the state for you - whenever it changes, it is your responsibility to refresh the Ribbon (by calling IRibbonUI.Invalidate), which will cause Outlook to invoke your callback on the toggle button to determine its state. You just need to store the appropriate state in your code.

How Can I Comprehensively Manage my VBA UserForm's Properties?

I am writing my first VBA Add-In under Microsoft Office Word 2007 with Windows 7 Pro/64. Part of this Add-In is a UserForm. Using the Visual Basic editor that runs from Word, I find there are two ways of viewing, and two ways of modifying a UserForm's properties:
View all properties from Object Browser (F2)
View some properties and edit them from Properties Window (F4)
Manually enter and edit any property from the Code window (F7)
Here is a screenshot of my Properties and Code windows:
A problem I find is that the Properties Window contains only a subset of the UserForm's properties (notice that CanPaste, CanRedo, and CanUndo don't appear in Properties), and changes made in the Properties Window are overridden by changes made in the Code window (e.g., at runtime, Me.Caption from the Code window above overrides the Caption field in Properties).
I suppose I should avoid using Properties at all then, and enter all settings via UserForm_Initialize as shown above. But (a) for some settings, Properties makes several settings at once. For example, selecting Verdana Bold from Properties equals Font = Verdana and Font.Bold = True in Code. And (b) it seems Properties sets the subset of properties it controls to defaults of its choosing, and if I change them I can't see what they started out as.
I therefore desire unified and comprehensive access to all my UserForm's properties at once, including the aforementioned default settings. Does anyone know how to reveal a UserForm's default settings as code, or to automatically open all its current settings in the Code window? Is there an umbrella mechanism I'm not aware of?
I'm not a veteran VBA programmer, but I can't believe my experience is unique. I've searched the 'net in vain for a solution. How do you with more experience manage this dilemma?
You use the Properties window to set appearance-related properties at design time. Those property values will then always apply unless you explicitly change them at run-time with VBA code.
Properties that don't relate to appearance, such as CanPaste and CanRedo relate to the state of the form at run-time, so it doesn't make sense to have them configurable at design-time.
You can change nearly all of the properties at run-time, whether it is in the Initialize event or elsewhere. You can even add controls at run-time, but your changes won't be persisted once the instance of the form terminates.

Make a ribbon button reference the macro in a new workbook instance rather than in the source template

I have a macro-enabled Excel template which contains a ribbon button pointing to a macro sub. The button assignment still points to the source template, however, instead of the sub in the new document. If I create a document and then move or delete the template, I'm no longer able to use the button in the new document:
I've noticed that the ribbon button seems have the macro's source template path hard-coded, and there isn't any place I can find in ribbon customization UI to change it:
I'm certain this has been asked before, but I've tried a number of different ways of searching for it without coming up with anything.
I found this which talks about a button on a form, not a ribbon, and I found this which seems to be generating "shapes" programmatically and assigning their action; neither of these two are relevant.
This is the closest I've found, but whereas the QAT customization tab has the option for setting the target:
the ribbon customization tab does not have that. Any help would be greatly appreciated.

Using Label property of Office Ribbon Control in vba

Is there a way to read/get the Label attribute of an Office 2007/2010 ribbon control in VBA? The only exposed properties appear to be Tag, ID, and Context.
I'm trying to use the getLabel and getVisible callbacks together--read the label and determine whether or not to show a control. I tried setting the Tag property in the getLabel callback, but the code won't compile as that property is read-only by then.
Any other suggestions for how to skin this cat welcome. Note that I cannot use a dynamic menu; this is under a split button, and has to stay there. Thanks in advance.
I suspect that VBA doesn't have access to customize the Ribbon UI. I do know for a fact that this can be handled with VSTO.