【问题标题】:Unhighlighting text (and preserve all other font settings)取消突出显示文本(并保留所有其他字体设置)
【发布时间】:2020-05-26 10:09:23
【问题描述】:

感谢 2 个帖子(herehere),我知道如何使用 VBA 代码在 PowerPoint 中突出显示文本框的文本。

但是,不突出显示文本的问题仍未解决。我尝试将未突出显示的文本框的属性设置为 TextRange2.Font(例如 .TextFrame2.TextRange.Font.Highlight.SchemeColor = -2),但尝试这样做时收到错误(键入的值超出范围)。 有人可以帮忙解决这个问题吗?

另外,当改变高亮颜色时 (例如TextRange2.Font.Highlight.RGB = RGB(255, 255, 175))我的文本框的格式发生了变化,因此字体的颜色从我预设的白色变为黑色,字体大小变小。有没有办法保留文本框的原始设置?这是由于访问 .TextRange2 而不是 .TextRange 而发生的吗?

感谢您的帮助!

【问题讨论】:

  • Highlight PropertyFont2 objectColorFormat 。其他ColorFormat 对象(例如Fill.ForeColorGlowFormat.Color)与另一个单独 值并存,以确定它是否可见(即Fill.VisibleGlowFormat.Radius)。我目前找不到可通过 VBA 访问的 Highlight 的任何等效项...
  • 感谢 Chronocidal 的检查,这真的很奇怪......

标签: vba powerpoint


【解决方案1】:

在 PowerPoint 2019/365 中,可以使用内置的 Mso "TextHighlightColorPickerLicensed" 删除突出显示。

此代码示例说明了如何取消突出显示选定形状中的文本。它找到包含突出显示的运行,选择它们并通过以编程方式调用命令栏的“突出显示”按钮来删除突出显示。

前提条件:PowerPoint 2019 或 365。演示文稿必须用窗口打开。

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

有时 Application.CommandBars.ExecuteMso() 方法可以访问通过 PowerPoint API 不可用的功能。 MsoId 显示在 PowerPoint 选项窗口的工具提示文本中:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-29
    • 2018-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多