vba check if shape name exists
To check if a shape name exists in VBA, you can use the following code:
Function ShapeExists(shapeName As String) As Boolean
Dim sld As Slide
Dim shp As Shape
ShapeExists = False
For Each sld In ActivePresentation.Slides
For Each shp In sld.Shapes
If shp.Name = shapeName Then
ShapeExists = True
Exit Function
End If
Next shp
Next sld
End Function
This code defines a function called ShapeExists
that takes a shapeName
as input and returns a boolean value indicating whether the shape exists or not. It loops through all the slides in the active presentation and checks each shape's name. If a shape with the specified name is found, the function sets the ShapeExists
variable to True
and exits the loop.
You can use this function in your VBA code like this:
Sub CheckShapeExistence()
Dim shapeName As String
shapeName = "MyShape"
If ShapeExists(shapeName) Then
MsgBox "Shape exists!"
Else
MsgBox "Shape does not exist!"
End If
End Sub
In this example, the CheckShapeExistence
subroutine assigns a shape name to the shapeName
variable and then calls the ShapeExists
function to check if the shape exists. Depending on the result, a message box is displayed.
You can replace "MyShape" with the name of the shape you want to check for existence.