【发布时间】:2020-05-20 14:08:57
【问题描述】:
我尝试做的是在 vba 中编写一个遍历所有文本/形状框的子程序,使用“间距”方法在单词中查找特定单词/字符并更改它们之间的空格。 出于某种原因,我不断收到同样的错误,我不知道如何解决。 例如,假设我有一个字符串 RLgsfub,并且我有一个单词列表 (RF,gs,Fg)。 子将找到 gs 并更改间距。
我尝试将一些东西结合起来,但我不明白为什么它仍然不起作用。 这是最后一个代码,当将其设置为 .Font.Bold = true 时,它可以工作,但在 .Font2.spacing = 2 的情况下它不起作用。 我尝试将 shp.TextFrame.TextRange 更改为 shp.TextFrame2.TextRange 但仍然无法正常工作。
Sub spacing():
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:="CompanyX")
Do While Not (foundText Is Nothing)
With foundText
.Font2.spacing = 0
Set foundText = _
txtRng.Find(FindWhat:="CompanyX", _
After:=.Start + .Length - 1)
End With
Loop
End If
Next
Next
End Sub
我尝试使用的其他代码:
这个工作正常:
Sub use()
Dim s As Slide
Dim shp As Shape
For Each s In ActivePresentation.Slides
For Each shp In s.Shapes
If shp.HasTextFrame Then
With shp
.TextFrame2.TextRange.Font.spacing = 24
End If
Next shp
Next s
End Sub
这个不行:
Sub HighlightKeywords()
Dim sld As Slide
Dim shp As Shape
Dim txtRng As TextRange, rngFound As TextRange2
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.TextFrame2.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
.spacing = -24
'~~> Find Next instance
Set rngFound = txtRng.Find(TargetList(i), n)
End With
Loop
Next
End If
Next
Next
End Sub
谢谢!
【问题讨论】:
标签: vba powerpoint spacing