如何删除VBA powerpoint中的部分文本框文本?

时间:2015-07-26 07:44:58

标签: vba formatting powerpoint powerpoint-vba

我有一个带有白色和黄色文字字幕的幻灯片演示文稿,每张幻灯片有1个文本框(白色显示,黄色显示)。我想用白点(“。”)替换白色文本。我是否需要创建变量并使其计数白色字符并从前面删除?

到目前为止,这是我的脚本:

Sub RemoveWhiteText()

    Dim oSl As Slide
    Dim oSh As Shape


    With ActivePresentation

For Each oSl In .Slides
    For Each oSh In oSl.Shapes
        With oSh
            If .HasTextFrame Then
                If .TextFrame.HasText Then
                    If TextRange.Font.Color = vbWhite Then
                        oSh.TextFrame.Text
                    End If
                End If
            End If
        End With
    Next
Next

    End With
End Sub

2 个答案:

答案 0 :(得分:0)

请阅读我对这个问题的评论。我建议在那里循环收集字符,直到字体的颜色为白色。

试试这个:

Sub RemoveWhiteText()

Dim oSl As Slide, oSh As Shape, oTr As TextRange, i As Long

For Each oSl In ActivePresentation.Slides
    For Each oSh In oSl.Shapes
        If oSh.HasTextFrame Then
            If oSh.TextFrame.HasText Then
                Set oTr = oSh.TextFrame.TextRange
                i = 1
                Do While oTr.Characters(1, i).Font.Color = vbWhite
                    i = i + 1
                Loop
                'MsgBox oTr.Characters(1, i - 1).Text
                If i > 1 Then oTr.Characters(1, i - 1).Text = "."
                Set oTr = Nothing
            End If
        End If
    Next
Next

End Sub

答案 1 :(得分:0)

每个不同格式的文本块都是Run。在您的情况下,第一次运行的字体颜色为白色。您可以使用该信息运行循环,如下所示:

Sub StripLeadingWhiteText()
Dim sld As Slide
Dim shp As Shape
Dim rn As TextRange2

For Each sld In ActivePresentation.Slides
    For Each shp In sld.Shapes
        If shp.HasTextFrame Then
           If shp.TextFrame2.HasText Then
                Set rn = shp.TextFrame2.TextRange.Runs(1)
                If rn.Font.Fill.ForeColor.RGB = vbWhite Then
                    rn.Text = "."
                End If
            End If
        End If
    Next
Next

End Sub`