Vba 在MS PowerPoint中查找并突出显示文本

Vba 在MS PowerPoint中查找并突出显示文本,vba,powerpoint,powerpoint-2007,Vba,Powerpoint,Powerpoint 2007,我用这个网站的一些代码制作了一个宏,在Word文档上进行关键字搜索并突出显示结果 我想在PowerPoint中复制这种效果 这是我的Word代码 Sub HighlightKeywords() Dim range As range Dim i As Long Dim TargetList TargetList = Array("keyword", "second", "third", "etc") ' array of terms to search for For i = 0 To UB

我用这个网站的一些代码制作了一个宏,在Word文档上进行关键字搜索并突出显示结果

我想在PowerPoint中复制这种效果

这是我的Word代码

Sub HighlightKeywords()

Dim range As range
Dim i As Long
Dim TargetList

TargetList = Array("keyword", "second", "third", "etc") ' array of terms to search for

For i = 0 To UBound(TargetList) ' for the length of the array

   Set range = ActiveDocument.range

   With range.Find ' find text withing the range "active document"
   .Text = TargetList(i) ' that has the words from the array TargetList
   .Format = True ' with the same format
   .MatchCase = False ' and is case insensitive
   .MatchWholeWord = True ' and is not part of a larger word
   .MatchAllWordForms = False ' and DO NOT search for all permutations of the word

   Do While .Execute(Forward:=True)
   range.HighlightColorIndex = wdYellow ' highlight the keywords from the for loop yellow

   Loop

   End With
Next

End Sub
这是我目前在PowerPoint中看到的内容,它根本不起作用

Sub HighlightKeywords()

Dim range As range
Dim i As Long
Dim TargetList

TargetList = Array("keyword", "second", "third", "etc") ' array of terms to search for

For Each sld In Application.ActivePresentation.Slides

For Each shp In sld.Shapes

    If shp.HasTextFrame Then

        Set txtRng = shp.TextFrame.TextRange

For i = 0 To UBound(TargetList) ' for the length of the array

   With range.txtRng ' find text withing the range "shape, text frame, text range"
   .Text = TargetList(i) ' that has the words from the array TargetList
   .Format = True ' with the same format
   .MatchCase = False ' and is case insensitive
   .MatchWholeWord = True ' and is not part of a larger word
   .MatchAllWordForms = False ' and DO NOT search for all permutations of the word

   Do While .Execute(Forward:=True)
   range.HighlightColorIndex = wdYellow ' highlight the keywords from the for loop yellow

   Loop

   End With
Next

End Sub

我最终通过MSDN找到了我的答案,但它与我从人们提交的内容中选择的正确答案非常接近

以下是我使用的代码:

Sub Keywords()

Dim TargetList
Dim element As Variant

TargetList = Array("First", "Second", "Third", "Etc")

For Each element In TargetList
   For Each sld In Application.ActivePresentation.Slides
      For Each shp In sld.Shapes
         If shp.HasTextFrame Then
            Set txtRng = shp.TextFrame.TextRange
            Set foundText = txtRng.Find(FindWhat:=element, MatchCase:=False, WholeWords:=True)
            Do While Not (foundText Is Nothing)
               With foundText
                  .Font.Bold = True
                  .Font.Color.RGB = RGB(255, 0, 0)
               End With
            Loop
         End If
      Next
   Next
Next element

End Sub


事实证明,该代码是有效的,但却是一场性能噩梦。下面我选择的正确答案代码运行得更平稳。我已经调整了我的程序以匹配所选的答案。

AFAIK没有内置的方法来用颜色突出显示找到的单词。您可以特意创建一个矩形形状,并将其放置在找到的文本后面并对其着色,但这完全是一种不同的球类游戏

下面是一个示例,它将搜索所有幻灯片中的文本,然后将找到的文本加粗、加下划线和斜体。如果需要,还可以更改字体的颜色

假设我们有一张幻灯片,看起来像这样

将此代码粘贴到模块中,然后重试。我已经对代码进行了注释,这样您在理解代码时就不会有问题

Option Explicit

Sub HighlightKeywords()
    Dim sld As Slide
    Dim shp As Shape
    Dim txtRng As TextRange, rngFound As TextRange
    Dim i As Long, n As Long
    Dim TargetList

    '~~>  Array of terms to search for
    TargetList = Array("keyword", "second", "third", "etc")

    '~~> Loop through each slide
    For Each sld In Application.ActivePresentation.Slides
        '~~> Loop through each shape
        For Each shp In sld.Shapes
            '~~> Check if it has text
            If shp.HasTextFrame Then
                Set txtRng = shp.TextFrame.TextRange

                For i = 0 To UBound(TargetList)
                    '~~> Find the text
                    Set rngFound = txtRng.Find(TargetList(i))

                    '~~~> If found
                    Do While Not rngFound Is Nothing
                        '~~> Set the marker so that the next find starts from here
                        n = rngFound.Start + 1
                        '~~> Chnage attributes
                        With rngFound.Font
                            .Bold = msoTrue
                            .Underline = msoTrue
                            .Italic = msoTrue
                            '~~> Find Next instance
                            Set rngFound = txtRng.Find(TargetList(i), n)
                        End With
                    Loop
                Next
            End If
        Next
    Next
End Sub
最终屏幕截图


我想扩展@Siddharth Rout的答案,这是一个很好的建议(我的获奖者+1)。然而,也有可能在PP中“突出显示”一个单词(一系列单词)。设置突出显示有一个严重的缺点——它会破坏其他字体设置。因此,如果一个人真的需要使用高亮显示,那么我们需要在之后返回适当的字体设置

以下是单个文本框中单个单词的示例:

Sub Highlight_Word()

Dim startSize, startFont, startColor

With ActivePresentation.Slides(1).Shapes(1).TextFrame2.TextRange.Words(8).Font
'read current state
   startSize = .Size
   startFont = .Name
   startColor = .Fill.ForeColor.RGB

'set highlight
   .Highlight.RGB = RGB(223, 223, 223) 'light grey

'return standard parameters
   .Size = startSize
   .Name = startFont
   .Fill.ForeColor.RGB = startColor

End With

End Sub

这种解决方案可以放在@Siddharth solution的某个地方。

如果您需要完全保留原始文本格式,您可以:

在查找包含目标文本的形状时, 复制形状 将副本发送到原始形状的Z顺序 在重复形状上进行高亮显示 将标签同时应用于副本和原件,以表明它们以后需要注意 例如 oOriginalShape.Tags.Add“Hilighting”、“Original” oDupeShape.Tags.Add“Hilighting”、“Duplicate”

将原始形状设置为不可见

然后,如果需要反转高亮显示并恢复原始格式,只需在所有形状之间循环;如果形状具有Hilighting标记=“原始”,请使其可见。如果它有higlight tag=“Duplicate”,则将其删除


这里的问题是,如果有人编辑了高亮显示的形状,则在还原时编辑内容将丢失。用户必须学会还原、编辑,然后重新=突出显示。

这看起来与我的想法非常接近,所以我认为我走的是正确的道路。谢谢你的帮助!哇,我希望我的代表能再给你一个+1。我编译你的代码只是为了好玩,真糟糕,它的运行速度是我的十倍。我想这就是你的for循环在列表中迭代查找每个嵌套文本框中的每个单词和我的迭代for循环在整个演示文稿中搜索一个单词,然后在整个演示文稿中再次搜索下一个单词之间的区别。再次感谢,通过你的例子,我学到了很多关于效率的知识-Ryant这基本上是我使用的方法,但我发现(无论如何,在PowerPoint 2013中)当找不到匹配项时,Find()函数不一定什么都不返回,而是可能返回一个空的TextRange对象。这似乎是一个PowerPoint错误。因此,我的变通代码相当于Do While Not rngFound is Nothing,并且rngFound.Length>0。很高兴知道高亮显示在技术上是可行的。感谢您的输入。“.Highlight.RGB=”行给了我以下错误:编译错误:找不到方法或数据成员非常确定您需要运行PPT 2010(或可能是2007)或更高版本才能使用。Highlight+1:)KJ。是的,
有可能突出显示我在帖子中提到的文本,但我知道没有像MS Word那样的内置方式…@Ryan,@Steve,仅在PP2010中测试过。我必须承认,
.Highlight
在PP帮助中描述得很差-使用时使用直觉:)