Page 1 of 1

Macro, copy a shape and paste it in another sheet

Posted: Thu May 23, 2019 3:01 am
by merouaneb
Hi, Community
I'm a new user in Open office, and i'm searching for resolving a problem that is structure like this:
I've a bitmap and from a sheet 1, i want to copy it and paste it in another sheet, and resizing to fit a 5 cells.
please help,me.

Re: Macro, copy a shape and paste it in another sheet

Posted: Thu May 23, 2019 3:19 pm
by Lupp
Why do you think you need a "macro"? Wherever possible do it without.

There are lots of specifics you need to consider for the task.
I will only post a raw specialised example. It assumes there is exactly one shape on the first sheet and NONE so far on the second.
It's complicated otherwise to identify the shapes to work on.
Concerning the size I don't worry about cell areas, but only demonstrate in principle how to set a shape's size to chosen values.

Code: Select all

Sub copyFirstShapeFromShee1ToSheet2CellD5()
doc0   = ThisComponent
cc     = doc0.CurrentController
dpr    = cc.Frame
dh     = createUnoService("com.sun.star.frame.DispatchHelper")
sheet0 = doc0.Sheets(0)
sheet1 = doc0.Sheets(1)
target = sheet1.GetCellRangeByName("D5")
dp0    = sheet0.DrawPage
dp1    = sheet1.DrawPage
shape0 = dp0(0)
cc.select(shape0)
dh.executeDispatch(dpr, ".uno:Copy",  "", 0, Array())
cc.select(target)
dh.executeDispatch(dpr, ".uno:Paste", "", 0, Array())
shape1 = dp1(0)
sz = shape1.Size
With sz
 .Height = .Height/2
 .Width = .Width/2
End With
shape1.setSize(sz)
shape1.Anchor = target
End Sub
See alao attachment.

Re: Macro, copy a shape and paste it in another sheet

Posted: Thu May 23, 2019 11:38 pm
by merouaneb
Hi,
Thank's for responding Lupp, Your solution work fine :bravo: but in my case when i try to modify the code to be suitable to my case just it doesnt work like i want.!!!!! :crazy:
This is a portion of my code, it's a function:

Code: Select all

Function flip_past(ranges,Calli)
doc   = ThisComponent
cc     = doc.CurrentController
dpr    = cc.Frame
dh     = createUnoService("com.sun.star.frame.DispatchHelper")
'sheet0 = doc.getSheets
'sheet1 = doc.sheets(5)
target = ThisComponent.getSheets.getByName("Doc").getCellRangeByName(ranges)
dp     = ThisComponent.getSheets.getByName("Legende").DrawPage
dp1    = ThisComponent.getSheets.getByName("Doc").DrawPage
shape = dp(Calli)
nom = ActiveWindow.Selection.ShapeRange.Name 'TextFrame.TextRange.BoundWidth
MsgBox (nom)

cc.select(shape)
dh.executeDispatch(dpr, ".uno:Copy",  "", 0, Array())
cc.select(target)
dh.executeDispatch(dpr, ".uno:Paste", "", 0, Array())
shape1 = dp1(Calli)
sz = shape1.Size

With sz
.Height = target.size.Height
.Width = target.size.Width
End With
shape1.setSize(sz)
shape1.Anchor = target    
 
End Function

Where Calli is the name of the shape, and ranges is a range from L12:L50
best regards, Yours.

Re: Macro, copy a shape and paste it in another sheet

Posted: Fri May 24, 2019 12:24 am
by Lupp
Sorry. I shouldn't have started this. Giving the above example I mainly intended to show how complicated even the simplest task of the kind under facilitating assumptions to the greatest extent would be.

I told you it's complicated and it is. This is not only concerning the identification of shapes. If you actually urgntly need to do things of the kind by macros, you need to learn to design them from the beginning. You need to understand some concepts of the API - and many details. Start studying the guide and the famous texts by Andrew Pitonyak - and be prepared to experience that many things were changed since, and that finding documentation is difficult.