'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!



Solution 1:[1]

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:

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1