重命名包含特定文本的形状

Rename shape containing specific text

Sub RenameShapeNameIfSpecificText()

Dim ppt As Presentation, sld As Slide
Set ppt = ActivePresentation

For Each sld In ppt.Slides

   Dim shp As Shape
   For Each shp In sld.Shapes

      If shp.TextFrame.TextRange = "0x" Then
      shp.Name = "Counter"
      End If

   Next shp
Next sld

End Sub

我有一个包含 20 张幻灯片的演示文稿,其中 18 张幻灯片的形状带有文本 0x。我想将这些形状重命名为“Counter”。

以上代码导致此错误:The Specified Value is out of range 我想错误是由于图像也存在引起的。

谢谢。

"Use the HasTextFrame property to determine whether a shape contains a text frame before you apply the TextFrame property":

Sub RenameShapeNameIfSpecificText()
    Dim ppt As Presentation, sld As Slide, shp As Shape
    Set ppt = ActivePresentation
    
    For Each sld In ppt.Slides
        For Each shp In sld.Shapes
            If shp.HasTextFrame Then
                If shp.TextFrame.TextRange = "0x" Then
                    shp.Name = "Counter"
                End If
            End If
        Next shp
    Next sld
End Sub