Trying to find a shape from a slide by name, should FindObject() be avoided at all costs?

To find a slide from the list of slides by name, one can use the .getByName() method.

Option Explicit
Sub Main
	Dim oSlideList as Object
	Dim oSlide as Object
	Dim oItem as Object
	oSlideList = ThisComponent.getDrawPages()
	oSlide = oSlideList.getByName("MyDesiredSlide")
	oItem = oSlide.getByIndex(27)
End Sub
Rem This works!

I wanted to find a shape within a slide by name but unintuitively this didn’t work.

Option Explicit
Sub Main
	Dim oSlideList as Object
	Dim oSlide as Object
	Dim oItem as Object
	oSlideList = ThisComponent.getDrawPages()
	oSlide = oSlideList.getByName("MyDesiredSlide")
	oItem = oSlide.getByName("MyDesiredShape")
End Sub
Rem This does not work
BASIC runtime error.
Property or method not found: getByName.

I found FindObject() which works but Pitonyak guide says it should not be used:

Option Explicit
Sub Main
	Dim oSlideList as Object
	Dim oSlide as Object
	Dim oItem as Object
	oSlideList = ThisComponent.getDrawPages()
	oSlide = oSlideList.getByName("MyDesiredSlide")
	oItem = FindObject("MyDesiredShape")
End Sub
Rem This works!

Do not use the functions FindObject and FindPropertyObject — they are poorly documented, buggy, and likely to be deprecated. I only mention the methods to be complete.

Is FindObject() still “poorly documented” and “buggy” in 2023? Is it harmless in my use case? The reason I do not want to use getByIndex() is that my slides are filled with shapes and finding objects by names is much easier when I have a good naming convention.

I don’t know anything about StarBasic function FindObject which is not documented at all. There is no getName because the names on a draw page are not guaranteed to be unique. Two or more objects on the same draw page can have the same name. Write your own function looping through the elements returning an array of all objects with the given name. This way you see how many of them exist, and in case of >1 you may be able to find the right one or eliminate duplicate names.

1 Like

@Villeroy Interesting. In Impress a slide and a shape can have the same name. But when I try to give an already used shape name to another shape, the graphical user interface does not let me do so regardless of whether they are on the same slide or not. Same goes for slides. Therefore I thought the names of objects contained in a single slide were guaranteed to be unique and a method to fetch by name would exist.

How can I write a looping function as you mentioned?