我使用本网站的一些代码制作宏来对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
结果证明代码有效,但这是一场性能噩梦。我在下面选择的正确答案的代码运行得更顺畅。我调整了我的程序以匹配所选的答案。
答案 0 :(得分:2)
AFAIK没有内置的方法来突出显示找到的带有颜色的单词。您可以选择创建矩形形状并将其放置在找到的文本后面并为其着色,但这完全是一个不同的球类游戏。
以下示例将搜索所有幻灯片中的文本,然后将找到的文本设置为BOLD,UNDERLINE和ITALICIZED。如果需要,还可以更改字体的颜色。
假设我们有一张看起来像这样的幻灯片
将此代码粘贴到模块中然后尝试。我已对代码进行了评论,以便您在理解代码时不会遇到任何问题。
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
最终截图
答案 1 :(得分:1)
我想延长@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解决方案的某个地方。
答案 2 :(得分:0)
如果您需要完全保留原始文本格式,您可以:
在找到包含目标文字的形状时, 复制形状 将副本发送到原始形状的Z顺序 在重复的形状上突出显示 将标签应用于副本和原始标签,以表明稍后需要注意 例如 oOriginalShape.Tags.Add“Hilighting”,“Original” oDupeShape.Tags.Add“Hilighting”,“Duplicate”
将原始形状设置为不可见
然后,如果您需要反转突出显示并恢复原始格式,您只需遍历所有形状;如果形状具有Hilighting标签=“原始”,则使其可见。如果它具有Higlighting tag =“Duplicate”,则将其删除。
这里的问题是,如果有人编辑了突出显示的形状,那么当您还原时,编辑将会丢失。必须教会用户恢复,编辑,然后重新突出显示。