如何使用VBA删除PPT中多余的空格?

How do I remove extra spaces in PPT using VBA?

谁能帮我写下面的代码?我正在尝试使用 VBA 删除 PowerPoint 演示文稿中的任何多余空格。

Sub removeSpaces()
Dim sld As Slide
Dim shp As Shape
For Each sld In ActivePresentation.Slides
    For Each shp In sld.Shapes
    Do While InStr(shp, "  ") > 0
        shp.Value = Replace(shp, "  ", " ")
        shp.Value = Trim(shp.Value)
    Next shp
Next sld
End Sub

当我当前 运行 它时,它显示 "Method or data member not found" 错误并突出显示 "shp.Value" 部分。

一个 Shape 对象没有 .Value 属性。 Instr 函数也可能不会针对 Shape 对象求值。

https://msdn.microsoft.com/en-us/library/office/ff747227(v=office.14).aspx

一般需要参考形状的TextFrame,如:

Dim shpText as String

For Each sld In ActivePresentation.Slides
    For Each shp In sld.Shapes
        If shp.HasTextFrame Then
            shpText = shp.TextFrame.TextRange.Text 'Get the shape's text
            Do While InStr(shpText, "  ") > 0
                shpText = Trim(Replace(shpText, "  ", " "))
            Loop
            shp.TextFrame.TextRange.Text = shpText 'Put the new text in the shape
        Else
            shpText = vbNullString
        End If
    Next shp    
Next sld
End Sub