Ask Your Question

Resize/Position Image Macro [Calc Macro Help] [closed]

asked 2016-06-17 05:20:30 +0200

Aimo gravatar image

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

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("")=false then
   msgbox "please select an image"
   exit sub
    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.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Ratslinger
close date 2016-06-18 03:49:27.888022

1 Answer

Sort by » oldest newest most voted

answered 2016-06-17 14:51:07 +0200

Ratslinger gravatar image

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
Dim aPosition As New
oCurrSel = ThisComponent.getCurrentSelection()
If oCurrSel.ImplementationName = "" Then
    oItem = oCurrSel.getByIndex(0)
    aPosition.X = 2200
    aPosition.Y = 2200
    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.

edit flag offensive delete link more


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

Aimo gravatar imageAimo ( 2016-06-18 03:24:07 +0200 )edit

Question Tools

1 follower


Asked: 2016-06-17 05:20:30 +0200

Seen: 988 times

Last updated: Jun 17 '16