gpt4 book ai didi

vba - 在 MS PowerPoint 中查找和突出显示文本

转载 作者:行者123 更新时间:2023-12-04 05:03:22 25 4
gpt4 key购买 nike

我使用这个站点的一些代码制作了一个宏来对 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 没有内置的方法 亮点 找到的带有颜色的单词。您可以特意创建一个矩形并将其放在找到的文本后面并为其着色,但这完全是一个不同的球类游戏。

这是一个示例,它将在所有幻灯片中搜索文本,然后将找到的文本设为粗体、下划线和斜体。如果你愿意,你也可以改变字体的颜色。

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

enter image description here

将此代码粘贴到模块中,然后尝试。我已经对代码进行了注释,以便您理解它不会有问题。

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

最终截图

enter image description here

关于vba - 在 MS PowerPoint 中查找和突出显示文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15844903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com