Ask Your Question

moving section in writer document using macro

asked 2020-04-19 11:25:43 +0200

torreone gravatar image

Is it possible, via macro, to move (not copy) a pre-existing section, therefore already created and inserted in the writer document? I thought this code was enough

oD= ThisComponent
refSection = oD.TextSections.getByName ( "sec1")
oV = oD.currentController.virtualCursor
oD.Text.insertTextContent (oV, refSection, False)

this code works well both to move textframes to the current position of the virtualCursor and to insert newly created sections On the contrary, with pre-existing section this code keep giving me error Type: " Message:." In the absence of the message I do not understand the error Thanks in advance for any suggestions

Strangely, I also have problems with the section copy & paste, which previously worked, but I'm still testing it.

LO: Version: SO; Ubuntu16.04.1

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2020-04-19 21:46:46 +0200

torreone gravatar image

updated 2020-04-19 22:38:51 +0200

It was not the answer I was looking for. It seems strange to me that, once inserted, it is not possible with a macro to move a textcontent object (the section object should be a textcontent) within the text unless you use the cut & paste

In the example below, for simplicity the document contains only one section which is moved within the document itself to the current position of the view cursor (mouse position)


1) one: Paste and one: Cut and in general the commands uno: xxxx are casesensitive

2) the section must be selected by selecting also a character before and after the section proper. Otherwise only the content of the section is moved, not the section as a whole

This is the code.

                          '  cut section with index 0 in current document   
                          '  and copy in current position of view cursor
 sub sezText01()
 end sub

Sub sezCopyPaste(oSec,optional cursPos, optional oD)   
   Dim oVC, oSezTC, oText
   Dim oDisp, oC, oF

   if isMissing(oD) then oD=thisComponent

   oDisp  =createUnoService("") 
   oC = oD.currentController:  oF= oC.frame
   oText = oD.getText()

   if isMissing(cursPos) then 
                                             ' textcursor cursPos on current viewCursor
      cursPos = oVC.getText.createTextCursorByRange(oVC) 
   end if
                                            ' create curson around oSec for selecting
   oSezTC= oText.createTextCursorByRange(oSec.Anchor.getStart()) 
   oSezTC.goLeft(1, false) 

   oDisp.executeDispatch(oF, ".uno:Cut", "", 0, Array())    
   oDisp.executeDispatch(oF, ".uno:Paste", "", 0, Array())

end sub
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2020-04-19 11:25:43 +0200

Seen: 80 times

Last updated: Apr 19 '20