How do I remove extra spaces in PPT using VBA?

与世无争的帅哥 提交于 2019-12-12 04:18:22

问题


Can anyone help me with the below code? I am trying to use VBA to remove any extra spaces in a PowerPoint presentation.

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

When I currently run it, it shows an error of "Method or data member not found" and highlights the "shp.Value" part.


回答1:


a Shape object doesn't have a .Value property. The Instr function may also not evaluate against a Shape object.

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

Generally, you need to refer to the shape's TextFrame, like:

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


来源:https://stackoverflow.com/questions/34495654/how-do-i-remove-extra-spaces-in-ppt-using-vba

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!