使用 VBA 删除文本的额外空格而不丢失幻灯片样式?
Removing extra spaces of a text without losing style of slide using VBA?
我正在尝试使用 trim 方法并在以下帮助下删除文本中的多余空格:
问题是,它正在更改字体颜色、样式并将幻灯片中的所有文本设置为单个 format/style。
示例:
幻灯片的标题为蓝色粗体,其余文本为黑色。在 运行 上面的代码之后,标题正在更改其样式(即更改为黑色而不是粗体)。
这是链接问题中代码的略微修改版本,它使用 TextRange.Replace
而不是将修剪后的文本写回形状。它应该保持格式。
Sub Test()
Dim shpTextRng As TextRange
Dim sld As Slide
Dim shp As Shape
For Each sld In ActivePresentation.Slides
For Each shp In sld.Shapes
If shp.HasTextFrame Then
Set shpTextRng = shp.TextFrame.TextRange
Do While InStr(shpTextRng.Text, " ") > 0
shpTextRng.Replace FindWhat:=" ", ReplaceWhat:=" "
Loop
End If
Next shp
Next sld
End Sub
我正在尝试使用 trim 方法并在以下帮助下删除文本中的多余空格:
问题是,它正在更改字体颜色、样式并将幻灯片中的所有文本设置为单个 format/style。
示例:
幻灯片的标题为蓝色粗体,其余文本为黑色。在 运行 上面的代码之后,标题正在更改其样式(即更改为黑色而不是粗体)。
这是链接问题中代码的略微修改版本,它使用 TextRange.Replace
而不是将修剪后的文本写回形状。它应该保持格式。
Sub Test()
Dim shpTextRng As TextRange
Dim sld As Slide
Dim shp As Shape
For Each sld In ActivePresentation.Slides
For Each shp In sld.Shapes
If shp.HasTextFrame Then
Set shpTextRng = shp.TextFrame.TextRange
Do While InStr(shpTextRng.Text, " ") > 0
shpTextRng.Replace FindWhat:=" ", ReplaceWhat:=" "
Loop
End If
Next shp
Next sld
End Sub