Change colour of highlighted text of a textbox in PowerPoint VBA - vba

So I want to have a button that changes the selected/highlighted text of a TextBox in PowerPoint VBA.
I know how to change all the text on a TextBox, but not the selected/highlighted.
Thanks for reading.

You'd want to ensure that there's a current selection and that the selection is text before trying to change anything:
With ActiveWindow.Selection
If .Type = ppSelectionText Then ' Text is selected
' Make it green, for example:
.TextRange.Font.Color.RGB = RGB(0, 255, 0)
End If
End With

Related

Unhighlighting text (and preserve all other font settings)

Thanks to 2 posts (here and here), I know how to highlight text of a textbox in PowerPoint with VBA code.
However, the problem of unhighlighting text remains unsolved. I tried to set properties of a non-highlighted textbox to TextRange2.Font (e.g. .TextFrame2.TextRange.Font.Highlight.SchemeColor = -2) but receive errors when trying so (The typed value is out of range).
Can someone help to solve this issue, please?
Additionally, when changing the highlight color
(e.g. TextRange2.Font.Highlight.RGB = RGB(255, 255, 175)) the formatting of my textbox changes, so the font is changing its color from my preset white to black and the font size gets smaller. Is there any way to preserve the original settings for the textbox? Is this happening due to the access of .TextRange2 and not .TextRange?
Thanks for your help!
In PowerPoint 2019/365 it is possible to remove highlight by using built-in Mso "TextHighlightColorPickerLicensed".
This code sample illustrates how to unhighlight text in selected shapes. It finds Runs containing highlighting, selects them and removes highlight by programmatically invoking Command Bar "Highlight" button.
Preconditions: PowerPoint 2019 or 365. Presentation must be opened with window.
Option Explicit
Sub UnhighlightTextInSelectedShape()
Dim sh As Shape
For Each sh In ActiveWindow.Selection.ShapeRange
UnhighlightTextInShape sh
Next
End Sub
Sub UnhighlightTextInShape(sh As Shape)
On Error GoTo Finish
Dim highlightIsRemoved As Boolean
Dim tf As TextFrame2
Set tf = sh.TextFrame2
Do
Dim r As TextRange2
highlightIsRemoved = True
For Each r In tf.TextRange.Runs
If r.Font.Highlight.Type <> msoColorTypeMixed Then
' Indicate that text contains highlighting
highlightIsRemoved = False
' The text to un-highlight must be selected
r.Select
If Application.CommandBars.GetEnabledMso("TextHighlightColorPickerLicensed") Then
' This Mso toggles highlighting on selected text.
' That is why selection must contain highlight of the same type
Application.CommandBars.ExecuteMso ("TextHighlightColorPickerLicensed")
' Unhighlighting May invalidate number of runs, so exit this loop
Exit For
Else
Exit Do
End If
End If
Next
Loop Until highlightIsRemoved
Finish:
If Not highlightIsRemoved Then
MsgBox "Unhighlighting is not supported"
End If
End Sub
Sometimes Application.CommandBars.ExecuteMso() method gives access to features not available via PowerPoint API.
The MsoId is displayed in tooltip text in PowerPoint options window:

How to select shape and run macro to fill color?

I am trying to select a shape and fill this shape with a color in a PowerPoint presentation like that (I put my code on the green button):
Sub ChangeInRed()
If ActiveWindow.Selection.Type = ppSelectionNone Then
MsgBox "Please selecte a zone !"
Else
For Each shp In ActiveWindow.Selection.ShapeRange
shp.Fill.ForeColor.RGB = RGB(255, 0, 0)
Next shp
End If
End Sub
If I run my code from macros-code, my selected shape changes to red; if I don't have a shape selected, my code displays: "Please select a zone".
All good, but in the edit mode, my button is not active to run this piece of code and I tried that in PRESENTATION mode.
When I try to select a shape in the presentation mode, my slideshow gets to the next page but I don't need that.
I want to select the shape and press the button to change the color to red, or I want to click on the shape and change the color.
If I try the code from the top in macro-code, my shape changes to red, but not in presentation.
My question is: how to select a shape in the presentation mode and change color?
If you want the shape to change colour during a slide show, copy the macro below to your PowerPoint .pptm (or .ppsm) file and then with the shape selected, go to the PowerPoint ribbon and click Insert / Action and in the Mouse Click tab, click Run Macro and make sure the ChangeShapeColour macro is selected before clicking OK.
Option Explicit
Sub ChangeShapeColour(ByRef oShp As Shape)
oShp.Fill.ForeColor.RGB = RGB(255, 0, 0)
End Sub

VBA, Fill shape with button click

I'm very new to macros and I'm trying to teach myself VBA in excel 2013 and could use a lot of help. I would like to know how to change a shape fill color (not a cell) when I click an ActiveX button. Here is what I'm thinking:
onClick() <-- Do I need a button name?
if shape.color = RGB(231,230,230) <-- this is the starting color
shape.color = RGB(0,0,0) <-- this is what I want to change it to
else
shape.color = RGB(231,230,230) <-- if color is black change to this
end if
By the way, this is for fun and not for anything special or official.
Here is the code you are looking for:
Option Explicit
Sub ButtonClick()
Dim shp As Shape
Set shp = ThisWorkbook.Worksheets(1).Shapes("Rectangle 1")
If shp.Fill.ForeColor.RGB = RGB(231, 230, 230) Then
shp.Fill.ForeColor.RGB = RGB(0, 0, 0)
Else
shp.Fill.ForeColor.RGB = RGB(231, 230, 230)
End If
End Sub
Just make sure that you adjust the above code to the correct shape name (which is "Rectablge 1" in this example, which is located on Sheet(1)).
Afterwards, create a button on that sheet and assign this macro to the button to make it work and toggle the shape color between black and grey.
Next time, I recommend that you (in a first step) record the things you want Excel to do for you with the VBA macro recorder: https://www.youtube.com/watch?v=Q_HQGHPBYoo Afterwards, have a look at the code and try to understand it by highlighting VBA key-words and pressing F1. Also, I'd recommend reading this: http://www.homeandlearn.org/ Afterwards you should be set to write some rather fancy macros.
Put this in the module of the worksheet which contains the button:
EDITED: Sorry, I originally thought you wanted the actual button colour to change. I borrowed from Ralph's answer to account for the shape being the object of the colour change, but below this is for an ActiveX control (as you asked) instead of a form button:
Private Sub CommandButton1_Click() ' Yes, you do need to specify the button
Dim shp As Shape
Set shp = ThisWorkbook.Worksheets(1).Shapes("Rectangle 1")
If shp.Fill.ForeColor.RGB = RGB(231, 230, 230) Then
shp.Fill.ForeColor.RGB = RGB(0, 0, 0)
Else
shp.Fill.ForeColor.RGB = RGB(231, 230, 230)
End If
End Sub
Yes, you need a button name. Drop an ActiveX control on the sheet, then go to the sheet's module in the VBE. In the left dropdown at the top of the code pane, find your control name (you probably want to rename it first - I didn't for this example)
When you pick the control name in the left dropdown and the event name in the right, the VBE stubs the Sub and End Sub for you.
Next you need to reference your shape in your code. Start by using the Me keyword. When you're in a sheet's code module, Me refers to that sheet. Then the dot operator will expose all the properties and methods of that sheet. When you drop an ActiveX control on a sheet, a new property of the sheet is automatically created that provides access to the object. You can use
Me.CommandButton1.BackColor = RGB(231, 230, 230)
You If logic looks fine.

How to change text colour when text box clicked in Macro enabled Powerpoint 2013?

So I would like the text to change colour from grey to black... How do I do this on Powerpoint 2013 and where would I apply the code? Sorry in advance for not supplying any code I just don't know where to start.
I figured it out myself...
NOTE: You have to set the ForeColor to a certain color before you begin the Slideshow Presentation.
This example shows how to change the preset color to black
Private Sub TextBox1_GotFocus()
TextBox1.ForeColor = RGB(0, 0, 0)
End Sub
.

disappearing text on mouse click in text box in powerpoint using vba

I am facing an issue with the custom textbox in a PowerPoint 2010 slide. I want to make the text that is initially there inside the textbox (say "Click to enter text") to disappear as soon as mouse is clicked on the textbox to enter some text. If this is possible through events in VBA, I would like to know which event(associated with the textbox) to capture and how to do it using VBA?
Also changing the height parameter is not having any effect in PowerPoint 2010.
here is my code that defines the textbox :
Dim sld As Slide
Set pShape = sld.Shapes.AddTextbox(msoTextOrientationHorizontal, Left:=50, Top:=50, width:=500, Height:=300)
pShape.TextFrame.TextRange = "Click to enter text"
pShape.TextFrame.TextRange.Font.Size = 14
pShape.Line.Visible = True
pShape.Line.ForeColor.RGB = RGB(0, 0, 0)
pShape.Line.DashStyle = msoLineDash
Plz help me out with this..
Thanks in advance!!!
The height is not taking because the default is to resize to fit availabl text.
Either use a shape not a textbox or reset the defaut
Set pShape = sld.Shapes.AddTextbox(msoTextOrientationHorizontal, Left:=50, Top:=50, Width:=500, Height:=300)
pShape.TextFrame.AutoSize = ppAutoSizeNone
pShape.Height = 300
Making the text disappear on click is possible but difficult. You can use the WindowSelectionChange event but this is very difficult to set up if you are not writing an addin. You would need to write a WithEvents Class module and initialise an instance. Fairly advanced!