Execute VBA from Excel 2010 Ribbon - vba

I have been trying different examples of how to move my VBA buttons into the ribbon but either I the text is flagging red(aka error) or examples relate to doing some xml conversions (never done before so they make no sense).
All I want to do is move my text coloring buttons into my custom ribbon. When the user clicks on the button icon(inside the Ribbon), the VBA coding executes.
As you can see above, currently the Text-Green button on the ribbon...
when the user clicks it.. it generates a blank Command Button for User to create new Command Buttons. Clearly not what I want.
Any suggestions?
My Blue and Red button coding (simple coding):
Private Sub Color_Blue_Click()
Selection.Font.Color = RGB(83, 141, 213)
End Sub
Private Sub Color_Red_Click()
Selection.Font.Color = RGB(255, 0, 0)
End Sub
I have tried referencing:
http://www.rondebruin.nl/win/section2.htm

Wow I guess it was a simple fix afterall:
I went to:
Developer Tab/Record Macro
Named the Macro as TextGreen.... clicked Ok and once it was saved I clicked Stop Recording
Went back to Developer/Code Tab and clicked on Macros
Then I clicked Edit and typed my code there:
Sub TextGreen()
Selection.Font.Color = RGB(0, 176, 80)
End Sub
To save to ribbon I did:
Right click Ribbon/Customize Ribbon... Selected Macros from drop down...saved a group under my Ribbon Tab and Added it.

Related

How to run the VBA code while selecting an drop down menu item

In my MS Word form, there are some drop down menus. By default "Yes" is the green and "No" is the red in color while the file is opened. I would like to change the font color of the drop down menu items while selected, to be Red in case of "No" and to be Green in case of "Yes".
For this purpose I have created a Document_ContentControlOnExitsub currently only for the "Did the AA's performance drop after the incident? question in the following MS Word form. But the code is not running while selecting the "Yes" from the drop down menu. I can write and run VBA code in MS Excel but new to MS Word VBA, thus confused how to run the code. Can any one help?
I also would like to create a VBA code which will check and make sure that all "No" are Red and "Yes" are Green while the file is opened. So I think, there should be a VBA which will run during the MS Word file opening and iterate through all the Drop Down menus for that purposes. But this is secondary, if solved, bonus but not so important, at this moment it is important to me to change the font color during selecting menu items.
Here is the VBA code:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
With ContentControl
If .Title = "incident" Then
Select Case .Range.Text
Case "Yes": .Range.Font.ColorIndex = wdRed
Case "No": .Range.Font.ColorIndex = wdBrightGreen
Case Else: .Range.Font.ColorIndex = wdAuto
End Select
End If
End With
End Sub
Here is the MS Word file:
https://drive.google.com/file/d/1Avjx8nubEIp9K9NwqtyfHvdohQEsQiP8/view?usp=sharing
Check which module your code is in.
Document_ContentControlOnExit is a document level event handler so it should be in the "ThisDocument" module.
Also note that in the tools area at the top of that module there are two drop down menus. These can be used to create event handlers for the document's objects and are useful to ensure that the syntax is correct.

Create radio buttons in word VBA

Is there a way to create a macro that, when activated, will present the user with some options, then call some other macro depends on what the user selected?
For example:
Message box: Are you a male or female?
Option 1: Male
Option 2: Female
If the user selects "Male," execute macro A. If "Female," macro B.
while in Word UI press ALT F11 to open VBA IDE
in main ribbon click Insert -> Userform
and there you have a toolbox (if not, click View->ToolBox) and a Userform canvas
from toolbox drag the option button icon and drop it into desired position of Userform. repeat it twice
select first option button in the userform, click on its caption (some "OptionButton1" is shown as default) and edit it to "Male"
do the same with second option button and edit its caption to "Female"
click twice on the first option button on the userform
it will show you
Private Sub OptionButton1_Click()
End Sub
then fill it as follows:
Private Sub OptionButton1_Click()
macroA
End Sub
click twice on the second option button on the userform
it will show you
Private Sub OptionButton2_Click()
End Sub
then fill it as follows:
Private Sub OptionButton2_Click()
macroB
End Sub
this will get you started

VBA auto hide ribbon in Excel 2013

How to Auto-hide Ribbon in Excel 2013 in VBA? I would like to achieve exactly what I get by clicking on the upper arrow icon at the right top of Excel menu marked with blue in the picture below and then clicking on the first option marked with orange:
I would be also interested in VBA switching back to the third option Show Tabs and Commands. Important thing for me is to keep in the Excel menu the upper arrow icon (marked with blue).
I have tried hints shown in this thread: VBA minimize ribbon in Excel
but I am not satisfied with results.
Attempt 1
Application.ExecuteExcel4Macro "Show.ToolBar(""Ribbon"",False)
This is good but hides the blue icon.
Attempt 2
CommandBars.ExecuteMso "MinimizeRibbon"
This is close to what I want. This keeps the blue icon but does not hide the entire menu. It switches to the second option displayed in the picture Show Tabs.
Attempt 3
SendKeys "^{F1}"
The attampt does not work at all. Moreover, it is supposed to imitate the attempt 2. So even that would not satisfy me.
I can't see that anyone else has brought this up... This isn't a workaround, this is the actual idMSO for what I think you're looking for. This code makes my excel window look like everything is gone the same way the first option does for Auto-Hide Ribbon.
Before the code runs, my window looks like this, in the 'Restore' size:
Running the following code:
Sub HideTheRibbon()
CommandBars.ExecuteMso "HideRibbon"
End Sub
Will make your window look like this, in the maxamized window size (just like what would happen if you were to press the Auto-Hide Ribbon button manually):
If you want the ribbon automatically hidden when the workbook opens, put this in the workbook code:
Sub Workbook_Open()
CommandBars.ExecuteMso "HideRibbon"
End Sub
Alternatively, to achieve the same thing, you could put this code in a module:
Sub Auto_Open()
CommandBars.ExecuteMso "HideRibbon"
End Sub
If you want the window to revert back to normal, you run the exact same code again. In other words, the following code would make no visual change at all when ran because the idMSO "HideRibbon" is a toggleButton:
Sub HideTheRibbon()
CommandBars.ExecuteMso "HideRibbon"
CommandBars.ExecuteMso "HideRibbon"
End Sub
If you want a full list of all the idMSO in excel, click the following that apply to you: Excel 2013+, Excel 2010, Excel 2007
I use this for presentation purposes
ActiveWindow.DisplayGridlines = False
ActiveWindow.DisplayHeadings = False
Application.DisplayFormulaBar = False
Application.DisplayFullScreen = True This is what i used to hide the ribbon
Probably you should do something a little more complicated:
Use CommandBars.ExecuteMso "MinimizeRibbon" to show/hide the ribbon.
Depending on what you want, you may show/hide all other tabs in the ribbon. E.g. use something of the code here -> Excel Hide/Show all tabs on Ribbon except custom tab
Thus 2 steps:
Step 1 - show or hide with the CommandBars.ExecuteMso
Step 2 - show or hide the rest of the tabs with some macros from the link.
A little big workaround, but you will get what you want.
I call this macro on Workbook_Open to check for the ribbon and if not hidden, it will hide the ribbon (I actually have it located in another Sub that also removes the formula bar, status bar, headings, and gridlines at Workbook_Open)...
Sub HideRibbon()
If CommandBars("Ribbon").Controls(1).Height < 100 Then
Exit Sub
Else
CommandBars.ExecuteMso ("MinimizeRibbon")
End If
End Sub
Then I call this macro on Workbook_BeforeClose to check for the ribbon and if it is not shown, it will show the ribbon for the next excel spreadsheet that is opened.
Sub ShowRibbon()
If CommandBars("Ribbon").Controls(1).Height > 100 Then
Exit Sub
Else
CommandBars.ExecuteMso ("MinimizeRibbon")
End If
End Sub
This eliminates the chance of hiding the ribbon when the workbook is opened and a user then manually showing the ribbon which in turn would reverse the show on close and actually hide the ribbon. On open, the ribbon would then be shown again. This will keep it the same every time on open and close of the workbook.
Give this a try:
Sub ShowHideRibbon()
If CommandBars("Ribbon").Controls(1).Height < 100 Then
CommandBars.ExecuteMso ("MinimizeRibbon")
Else
CommandBars.ExecuteMso ("MinimizeRibbon")
End If
End Sub
Or this:
Sub ShowHideRibbon1()
If Application.ExecuteExcel4Macro("Get.ToolBar(7,""Ribbon"")") Then
Application.ExecuteExcel4Macro "Show.ToolBar(""Ribbon"", False)"
Else
Application.ExecuteExcel4Macro "Show.ToolBar(""Ribbon"", True)"
End If
End Sub
First, go to the Excel Options, and go to "Quick Action Toolbar".
From there, search for "Hide Ribbon" and add to the toolbar. After it's on the QAT, you can call it quickly with ALT+# (on my computer it's the 8th thing, so ALT+8 will auto-hide).
Then just add a sub that does SendKeys ALT then 8:
Sub Macro1()
ActiveSheet.Activate
'Cells(1, 1).Select
SendKeys "%0", True
SendKeys "8", True
End Sub
Note: I know it's silly to have ActiveSheet.Activate, I just added that to test the macro. Depending on how it's being called, you can remove/comment out that line. The % is equivalent to ALT, and technically, I have to press 0 then 8, hence the two lines.
To get this code to work in excel 2016 you will need the following code in the "ThisWorkbook" mode.
Credit goes to BigBen - not me
Private Sub Workbook_Open()
application.CommandBars.ExecuteMso "HideRibbon"
End Sub

Macro enabled button VBA

I'm working with VBA macros and I have a macro enabled button of which I want to disable from being clicked when the workbook is first opened. Is there anyway to do this. The macro behind it just leads to another worksheet. ![enter image description here][1]
Any help would be great
Thanks
If your button is an ActiveX Command Button, put the following code in ThisWorkbook:
Private Sub Workbook_Open()
Worksheets("Sheet1").OLEObjects("CommandButton1").Enabled = False
End Sub
Change "Sheet1" to reference the sheet that contains the button and change "CommandButton1" to reference the name of the button.

How to get selected text in VBA

I have a macro that changes the selected text, and I have it assigned to a button.
It works perfectly when i run it directly from visual basic, but when I click the button, the button gets the focus and my text is no longer selected so the macro change the selected element to (button).
How can I select the text and run the macro by clicking on the button and still have the text selected?
The way to do this is to set the set the TakeFocusOnClick property of the CommandButton to False. Here are is the code I use.
Private Sub CommandButton1_Click()
Dim Sel As Selection
Set Sel = Application.Selection
If Sel.Type <> wdSelectionIP Then
MsgBox Sel.Text
End If
End Sub
Is the button embedded in the document? You may need to put it on a form that loads on top of the Word window or in a menu/toolbar, so that clicking it does not affect the selection in the document itself.
Edit:
I think you can use Application.Selection.Previous to get at what you need. You could use this to restore the selection after the click event, or to act upon that section of the document, or both.
I assume that this is available in previous versions of Word, but have only confirmed its presence in 2007.
You need to change TakeFocusOnClick to "False" in the Button Preferences.