我使用本网站的一些代码来制作一个宏,在Word文档上进行关键字搜索并突出显示结果。在MS PowerPoint中查找并突出显示文本
我想在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
原来,代码工作,但性能噩梦。我在下面选择的正确答案的代码运行得更顺利。我调整了我的程序以匹配选定的答案。
这看起来非常接近我的想法,所以我认为我走在了正确的道路上。谢谢您的帮助! – 2013-04-07 00:43:46
哇,我希望我有代表给你另一个+1。我编译你的代码仅仅是为了有趣和神圣的废话,它的运行速度是我的十倍。我想这就是循环迭代遍历列表的循环,它会在每个嵌套的文本框中查找每个单词,迭代循环搜索整个演示文稿中的一个单词,然后再次搜索整个演示文稿以查找下一个单词。 再次感谢,我通过您的示例了解了很多关于效率的知识。 -Ryan – 2013-04-07 01:19:14
这基本上是我使用的方法,除了我发现(在PowerPoint 2013中,无论如何),Find()函数不一定会在没有找到匹配项时返回Nothing,并且可能会返回一个空的TextRange对象。这看起来像是一个PowerPoint错误。因此,我的解决方法代码相当于Do While Not rngFound Nothing并且rngFound.Length> 0. – OfficeAddinDev 2016-10-05 12:00:12