Create Macro to Delete Shape in Draw

Can someone please help me to create a macro to remove the last shape and its contents in draw,
The Script i am currently trying looks as follows, but does not want to work.|

Sub DeleteLastShape()
Dim oDoc As Object
Dim oPage As Object
Dim oShapes As Object
Dim i As Integer
Dim lastShapeIndex As Integer

oDoc = ThisComponent

For i = 0 To oDoc.getDrawPages().getCount() - 1
    oPage = oDoc.getDrawPages().getByIndex(i)
    oShapes = oPage.getShapes()
    
    If oShapes.getCount() > 0 Then
        lastShapeIndex = oShapes.getCount() - 1
        oShapes.remove(lastShapeIndex)
    End If
Next i

End Sub

it gives the following error:
BASIC runtime error.
Property or method not found: getShapes.

I suggest you to install one of the excellent Object Inspection Tools: XrayTool or MRI. Then you will able to examine the existing properties and methods of the programming objects.

1 Like

Note: The index of the shapes can be modified by the Arrange feature. The “Bring to Front” will set the index to the maximum on the Drawpage. Therefore the index is not an exact information to determine the “lastly embedded” state after using the Arrange feature.

Sub DeleteLastShape()

 Dim oDoc As Object
 Dim oDrawPages As Object
 Dim oPage As Object
 Dim oShape As Object
 Dim PageCount As Object
 Dim ShapeCount As Object
 Dim i As Integer
 Dim lastShapeIndex As Integer

	oDoc = ThisComponent
	oDrawPages = oDoc.getDrawPages()
	oPageCount = oDrawPages.getCount()
	
	For i = 0 To oPageCount - 1
    	oPage = oDoc.getDrawPages().getByIndex(i)
    	'xray oPage
    	iShapeCount = oPage.getCount()
    	iLastShapeIndexOfPage = iShapeCount - 1
    	oLastShape = oPage.GetbyIndex(iLastShapeIndexOfPage)
    	'xray oLastShape
    	If iShapeCount > 0 Then
        	oLastShape.dispose
    	End If
	Next i
End Sub

This code will remove the shapes of each drawpages, what shape has the highest index on the actual drawpage.

Thank you that worked marvellous, I will now attempt to save the file as an pdf after executing the macro. It worked great, thanks.