如何选择格式化文本框中的单词的主动选择

时间:2019-07-09 05:04:56

标签: vba powerpoint

我正在尝试探索如何对文本框中的几个选定单词应用某种格式,但到目前为止我自己还无法完成。

通过下面创建的代码,我只能使用它来选择文本框中的所有单词,而不仅仅是我想要的几个单词。

如果有人能为我提供一个更简单/现有的代码可以帮助我解决这个问题,那将是很好的事情吗?

预先感谢

Sub ActiveTextRange()

Dim sld As slide
Dim sh As Shape
Dim wordcount As Long, j As Long, x As Long, y As Long, z As Long

wordcount = ActiveWindow.Selection.ShapeRange(1).textFrame.TextRange.Words.Count

With ActiveWindow.Selection.ShapeRange(1)
.textFrame.TextRange.Words(Start:=1, Length:=wordcount).Font.Color.RGB = RGB(230, 0, 0)
End With

End Sub

3 个答案:

答案 0 :(得分:0)

以下内容可能会有所帮助。这样做的关键是能够在较大的文本块中跟踪要更改的特定文本的位置。我的建议是在将文本添加到图形中时对其进行格式化。干杯。

Option Explicit

Sub ActiveTextRange()

    Dim vPresentation As presentation
    Dim vSlide As Slide
    Dim vShape As Shape
    Dim vAddThisText As String

    ' Create a new presentation, add a slide and a rectangle shape
    Set vPresentation = Application.Presentations.Add
    Set vSlide = vPresentation.Slides.Add(vPresentation.Slides.Count + 1, ppLayoutBlank)
    Set vShape = vSlide.Shapes.AddShape(msoShapeRectangle, 10, 10, 600, 300)

    ' Make the shape white with a 3pt dark red border
    vShape.Fill.ForeColor.RGB = rgbWhite
    With vShape.Line
        .ForeColor.RGB = rgbDarkRed
        .Weight = 3
    End With

    ' Setup the shape to be left aligned, font color, top anchored, etc
    With vShape.TextFrame
        .TextRange.Paragraphs.ParagraphFormat.Alignment = ppAlignCenter
        .TextRange.Font.Color.RGB = rgbBlack
        .VerticalAnchor = msoAnchorMiddle
        .TextRange.ParagraphFormat.SpaceAfter = 6
        .TextRange.ParagraphFormat.WordWrap = msoCTrue
    End With

    ' And now format the word red, which is the 7th character and is 3 long
    vAddThisText = "Hello Red World"
    vShape.TextFrame.TextRange.InsertAfter vAddThisText
    With vShape.TextFrame.TextRange.Characters(7, 3)
        .Font.Color.RGB = rgbRed
        ' and change other attributes if needed etc
    End With

End Sub

输出是...

screenshot

答案 1 :(得分:0)

这会将“标题”占位符中的第二个和第三个单词涂成红色。在单词之后,第一个数字是起始位置,第二个数字是长度:

Sub ColorWords()
  Dim objSlide As Slide
  Dim objShape As Shape
  For Each objSlide In ActivePresentation.Slides
    For Each objShape In objSlide.Shapes
      If objShape.Type = msoPlaceholder Then
        If objShape.PlaceholderFormat.Type = ppPlaceholderTitle Or objShape.PlaceholderFormat.Type = ppPlaceholderCenterTitle Then
          With objShape.TextFrame2.TextRange.Words(2, 2).Font.Fill
            .Solid
            .ForeColor.RGB = RGB(255, 0, 0)
          End With
        End If
      End If
    Next objShape
  Next objSlide
End Sub

要为单词选择着色,请使用:

ActiveWindow.Selection.TextRange.Font.Color.RGB = RGB(Red:=255, Green:=0, Blue:=0)

答案 2 :(得分:0)

好。我想我更好地理解了这个问题...但是我假设在此答复中您选择的是文本...,而不只是形状本身。因此,您正在编辑PowerPoint,在形状中选择一些文本,并想要运行宏以进行格式化(?),它应该与在代码模块中创建以下代码一样简单(然后我创建了一个自定义访问工具栏链接,以运行PowerPoint顶部的宏以使其变得快速):

Option Explicit

Sub ActiveTextRange()
    ActiveWindow.Selection.TextRange.Font.Color.RGB = rgbRed
End Sub

之前:

before

选择文本“红色”并运行宏:

after

顺便说一句...如果您只想选择形状并有逻辑地选择文本,那么这个概念就是它与我的第一个答案的结合。