我使用本網站的一些代碼來製作一個宏,在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