Resize/Position Image Macro [Calc Macro Help]

I’ve been trying and failing to make a macro to resize/position images in LibreOffice Calc and was hoping someone else might be able to write one for me. I’ve taken a look at this thread and this one, but I haven’t produced anything that works. I am using 5.0.6.3.

After dragging and dropping in image into Calc, I’d like to be able to run a macro on the selected image to do a few things:

  1. Resize it to 3.23" width, 1.74" height (keeping aspect ratio)
  2. Change the x position to 1.05"

So far I have this:

Sub ChangeSize
Dim g
    g = ThisComponent.CurrentSelection()
if g.supportsService("com.sun.star.text.TextGraphicObject")=false then
   msgbox "please select an image"
   exit sub
else
    g.Width = 3.23

end if
End Sub

But I can’t get past the first part to see if the second part works… I select an image in LibreOffice Calc and run the hotkey for the macro which I’ve bound to ctrl+shift+N and the message box comes up saying please select an image even though I have one selected.

This is a crude example to move and size an image:

Sub MoveResizeGraphic
Dim oCurrSel As Object
Dim oItem As Object
Dim aSize As New com.sun.star.awt.Size
Dim aPosition As New com.sun.star.awt.Point
oCurrSel = ThisComponent.getCurrentSelection()
If oCurrSel.ImplementationName = "com.sun.star.drawing.SvxShapeCollection" Then
	oItem = oCurrSel.getByIndex(0)
	aSize.width=2200
	aSize.height=2400
	oItem.setsize(aSize)
	aPosition.X = 2200
	aPosition.Y = 2200
	oItem.setposition(aPosition)
Else
	MsgBox "Image not selected"
End If
End Sub

Note that images are in pixels (up to you to convert) and positioning is by x & y location (again in pixels). This is only to get you started. Size and position in code is not to your specs. There are many other considerations such as multiple selected images and images already positioned at the same location.

This is perfect! Thank you so much. Now I just need to figure out how big it is in pixels. :smiley: