如何使用VBA删除PPT中的额外空格?

时间:2015-12-28 14:48:19

标签: vba powerpoint

任何人都可以帮我处理以下代码吗?我试图使用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

当我当前运行它时,它显示错误"未找到方法或数据成员"并突出显示" shp.Value"一部分。

1 个答案:

答案 0 :(得分:0)

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