Excel VBA hiding custom ribbon tab - vba

I have a custom ribbon which works fine but I only want to enable it and have my add in show for certain workbooks so I check for the workbook title on load and try to use the Invalidate method if the condition is false. Unfortunately nothing happens the custom ribbon tab is still showing.
The following is my sub:
Public Sub loadMyRibbon(ribbon As IRibbonUI)
Set RibUI = ribbon
If Not workbookTitle = "My Workbook" Then
If Not RibUI Is Nothing
RibUI.Invalidate
MsgBox "Not Working"
End If
End If
End Sub
Which seems correct to me from reading through the method documentation:
Microsoft Documentation
I see my MsgBox message displayed on the screen so I know the code is executing correctly up to that point but RibUI.Invalidate doesn't hide my tab. Appreciate any pointers!
I have also tried:
RibUI.InvalidateControl "myTag"
But this also doesn't work

Ribbon.Invalidate doesn't mean the ribbon will not show. Invalidate function just tells the ribbon to invalidate and re-initialize the ribbon controls with their default/dynamic properties.
I worked with few Add-ins where the clients wanted to hide the ribbon items if users cannot pass the authentication. So in such a case, I used "GetVisible" attribute in all of my Control and then I used this code
Sub GetVisible(control As IRibbonControl, ByRef Visible)
On Error Resume Next
Visible = shouldShowOrNot
End Sub
shouldShowOrNot is a boolean variable, which I set in Ribbon Load to true if user passes the authentication. See the following image:
Now the above image is a representation of Ribbon in case User has failed the authentication. There might be a better way to do it, but i found it to be the best way so far.
Hope this helps,
Vikas B

Related

Refreshing custom ribbon button which relies on a table to get the label and image

I have a custom ribbon and one of the buttons on it (image and label for the button) are supposed to change based on what happens in one of the forms (so, essentially, it's supposed to change when values in a table change and making changes in the form is the trigger for the change).
On the CLOSE button of the form I have the following code:
sbRefreshRibbon
MyRibbon.Invalidate
and here is what the procedure does, so the MyRibbon.Invalidate part is probably redundant:
Public Sub sbRefreshRibbon()
On Error GoTo RestartApp
MyRibbon.Invalidate
On Error GoTo 0
Exit Sub
RestartApp:
MsgBox "Please restart Application for Ribbon changes to take effect", _
vbCritical, "Ribbon Refresh Failed"
End Sub
In any case, sometimes, when clicking CLOSE I get the "Please restart Application for Ribbon changes to take effect" error. After restarting, all is well, the label is correct and so is the image
but, how can I make the label and image change without errors and restarts?
There is no need to call the Invalidate method of the IRibbonUI interface multiple times. You need to left a single call as soon as your form is closed.
Also you may consider using the InvalidateControl method of the IRibbonUI interface instead. It allows invalidating the cached value for a single control (not the whole custom UI) on the Ribbon user interface.

How to run code in a custom appointment tab?

I'm trying to write an extension to my Outlook calendar - More specifically the appointment module. (Form designer)
I have a button in my designer, that I want to write some simple code around.
Sub CommandButton1_Click()
msgbox "Hello World"
End Sub
The above code is put into the code editor that appears when I press "View code". Nothing happens.
How do I get a commandbutton to trigger a simple alert in the Outlook form designer?
Custom form script is now disabled by default. You can see more details for the known issue and you can re-enable it as the following link suggests.
You can read more about that in the Custom Form Security Changes article

Using VBA to allow a checkbox to hide/show content in Microsoft Word

I've gone through a number of tutorials and instructional videos trying to achieve my intended result of simply allowing a checkbox in my form to hide content when selected, or re-show it when being de-selected, but nothing seems to be working.
Currently, I've created a bookmark for the content I want hidden, and try to call his this in VBA with the following statement - which a number of resources indicated as the solution:
Private Sub CheckBox1_Click()
ActiveDocument.Bookmarks("bookmark").Range.Font.Hidden = CheckBox1.Value
End Sub
But despite doing this, selecting the checkbox has no affect on the content.
Is there something additional I'm missing (I'm using Microsoft Word 2013).
Your code worked fine when I tested it, but I ran into a few issues since I've never messed with userforms/checkboxs in Word VBA and I suspect you may have the same.
For instance, the first thing I did was create Insert --> Module. This is incorrect. What you want to do is Insert --> Userform then drag a checkbox over from the ToolBox
https://smallbusiness.chron.com/use-check-boxes-word-54673.html
Then double click the checkbox to bring up the "module" it would run, notice there is no module in the side pane! Edit the module, then go back to the userform and press F5. You should be presented with a checkbox that will hide/unhide your text.
Here is my module:
Public Sub CheckBox1_Click()
ActiveDocument.Bookmarks("bookmark").Range.Font.Hidden = CheckBox1.Value
End Sub
Here is an image:
Note: I didn't test how to insert the checkbox into the word doc, I'll leave you some of the learning!
Update:
This sub will make the CheckBox appear when run, but I'm not sure the logic you would use to call it, perhaps an event like opening of document?
Sub Loadform()
Load UserForm1
UserForm1.Show
End Sub
This could be called via a keyboard shortcut or event, but this will cause a "pop-up window". If you want an inform checkbox you may need to look into using this Legacy Form/Checkbox. I was following the URL from above but I think it's dated.
Update:
You could also add this easily to a toolbar, but that isn't likely what you want. I found the best way is to use a "field code" see --> https://word.tips.net/T001571_Assigning_a_Macro_to_a_Button_in_Your_Text.html
I was able to get this to work by pressing CTRL + F9 then typing { MacroButton Loadform ClickMe} then clicking this text. This may be the best bet as using an image seems not to be a great idea.. (https://answers.microsoft.com/en-us/msoffice/forum/all/using-graphic-with-macrobutton/a9c1ef3b-cf1f-48ba-92a8-c44bffcdc131) & (http://www.addbalance.com/usersguide/parts/macrobutton_fields.htm#Different_behavior)
Gif Example:

Click/DoubleClick to open a Userform on Word

I was just creating a template on Word which uses content controls and I have a userform to help auto fill the data which is activated via a command key. You know how on excel VBA you can click a cell to make it refresh or open up a userform, or perform a series of tasks. On Word, can you double or single click a Content Control per say and have a userform pop up?
I think it would be pretty neat to be able to do that, open for any ideas that I should try out.
Unlike Content Controls, ActiveX controls have Click and DoubleClick events. You can add an ActiveX control (e.g., a button or a label) and use its Click event to do whatever you want:
And then simply, use something like this:
Private Sub lblTest_Click()
MsgBox "You clicked on a label."
End Sub
Private Sub btnTest_Click()
MsgBox "You clicked on a button."
End Sub
Result:
Hope that helps.

Excel Addins - Not Modifying Toolbar

I am looking for the best way to deploy Excel Macros to users. My goal is to make it super easy for end users to install and promote use by adding to the addin toolbar. I know that there are a number of help articles on this topic but couldn't find anything that covered this exact issue. Can you please help and excuse me if this is a noobie question. Please see below for replication steps for my issue.
I have added the code below as a worksheet event on "This Worksheet" of an excel macro file
I add the main code to a module that it references
I save this as an .XLAM in the addin roaming folder
I enable this as an addin in EXCEL 2013
After I install it adds the button to an add in tab
It works until I close Excel in which case the button disappears
It is still under active add ins but not on the toolbar
The code:
Option Explicit
Dim cControl As CommandBarButton
Private Sub Workbook_AddinInstall()
On Error Resume Next 'Just in case
'Delete any existing menu item that may have been left.
Application.CommandBars("Worksheet Menu Bar").Controls("Super Code").Delete
'Add the new menu item and Set a CommandBarButton Variable to it
Set cControl = Application.CommandBars("Worksheet Menu Bar").Controls.Add
'Work with the Variable
With cControl
.Caption = "Convert Survey Reporter Tables"
.Style = msoButtonCaption
.OnAction = "CMB_General_Table_Formatting"
'Macro stored in a Standard Module
End With
On Error GoTo 0
End Sub
Private Sub Workbook_AddinUninstall()
On Error Resume Next 'In case it has already gone.
Application.CommandBars("Worksheet Menu Bar").Controls("Convert Survey Reporter Tables").Delete
On Error GoTo 0
End Sub
If you want an alternative to using VBA to build the interface, I have previously deployed Excel add-ins (XLAM files) using some variety of Ribbon XML. This allows for very fine-grained control of the resulting interface and does not require you to work in VBA to build the interface. For most applications, I have found it is much easier to build the Ribbon components outside of VBA and then wire up the callbacks in VBA.
For the end user, I think this approach also delivers a better looking add-in since the resulting interface has its own Ribbon tab (or you can add to any of the existing ones) instead of being in the Add-ins Ribbon tab.
If you want to pursue this route, I highly recommend using the Ribbon X Visual Designer to build the interface and set callbacks. I have used it to build an add-in that had more than 50+ features accessible by buttons and other Ribbon form controls. It was fairly painless once I got going.