Back

LibreOffice’s Basic - Objects position and size

How do we change the size and position of an object?

Normally we do that with the GUI, but here’s how to do this programmatically using Basic.

standard component properties are in :

thisComponent.DrawPage.Forms.getByName(<form name>).getByName(<component name>)

Theoretically, its position and size should be there too, right? Unfortunately, no. The positionning and size properties are elsewhere. They are in thisComponent.DrawPage.getByIndex(). The index is not the same as in the normal Xform component though, so you can’t just fetch the index there and search that index in DrawPage.

Certain (and yes, it’s really certain components) contain a variable called “Control” that links to the above component element. So you can actually get the name of the component. The problem is that not all elements in the indexes actually contain the variable “Control”. We actually have to filter all the elements by their ShapeType. Say we loop all elements to look for say lblTitle. Our looping variable is ‘i’. We would do something like this :

if obj.getByIndex(i).getShapeType() = "com.sun.star.drawing.ControlShape". If that is true, then

we are garanteed that the variable Control is available. We then do something like this :

if obj.getByIndex(i).Control.Name = "lblTitle" then ...

At that point, we can look up exactly the element for which we want to either get or set the position or size. The 2 variables we are interested in are : Position and Size. Position contains X and Y and Size contains width and height. You just need to assign position or size to an Object variable then you can change it’s value and push it back into the component. Like so :

(say the component object we got from earlier was put into the variable shapeObj)

Dim oPos AS Object
Dim oSize AS Object

oPos = shapeObj.Position
oSize = shapeObj.Size

oPos.X = oPos.X + 5
oSize.width = oPos.width + 5

shapeObj.Position = oPos
shapeObj.Size = oSize

And that’s it! You can move and resize any of your components from your scripts!

Happy Hacking!