[Solved] Moving Paragraphs in Writer

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
JeJe
Volunteer
Posts: 2784
Joined: Wed Mar 09, 2016 2:40 pm

[Solved] Moving Paragraphs in Writer

Post by JeJe »

I'm expecting a no here... but is there a way to move paragraphs in code?

***

There is an interface XTextRangeMover
makes it possible to move a text range (e.g. a paragraph by itself) within this text.
The movement is specified by the number of paragraphs within the order of paragraphs.
moveTextRange moves the contents to which xRange refers forward or backward.
https://www.openoffice.org/api/docs/com ... eTextRange

Mri lists this as an interface for thiscomponent.text but moveTextRange doesn't appear in the list of methods.

***

There is the ".uno:MoveDown" dispatch call which is the same as Ctrl+Alt+down (or up) arrow. This just moves the paragraphs up or down 1 paragraph though. I've attempted various names for a property that might move the paragraphs more than 1 at a time... but it seems that's all it can do.

Code: Select all

dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "ParagraphNumber" 'TRIED VARIOUS THINGS HERE
args1(0).Value = 2

dispatcher.executeDispatch(document, ".uno:MoveDown", "", 0, args1())
***

I know there's copy and paste and gettransferable but these all involve deleting and reinserting.


Edit: And drag and drop and using the navigator - but these aren't by code.
Last edited by robleyd on Tue Jan 21, 2020 1:19 pm, edited 2 times in total.
Reason: Add green tick
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
hubert lambert
Posts: 145
Joined: Mon Jun 13, 2016 10:50 am

Re: Moving Paragraphs in Writer

Post by hubert lambert »

Hi,

It's seems that the method moveTextRange was never implemented for the Text service : https://opengrok.libreoffice.org/xref/c ... 95cff#1962.
Regards.
 Edit: And this confirms: https://bz.apache.org/ooo/show_bug.cgi?id=85476
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
JeJe
Volunteer
Posts: 2784
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] Moving Paragraphs in Writer

Post by JeJe »

Thanks! Cut and paste or Xtransferable it will be...
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2784
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] Moving Paragraphs in Writer

Post by JeJe »

A sub to move something a specified number of paragraphs is probably not all that useful anyway (as opposed to one that moves something to another textrange) but:

Code: Select all


sub moveSelectionTransferable(noparas as long)
	
	doc = thiscomponent
	controller = doc.currentcontroller
	
	ovc= controller.viewcursor
	if ovc.string ="" then exit sub
	
	on error goto hr
	doc.lockcontrollers	
	trans1= controller.gettransferable
	ovc.string = ""

	otext = doc.gettext
	oCursor = oText.createTextCursorByRange(ovc)

	for i = 1 to abs(noparas)
		if noparas <0  then
			succeed =oCursor.gotoPreviousParagraph(false)
		else
			succeed = oCursor.gotoNextParagraph(false)
		end if
		if succeed = false then exit for
	next
	
	controller.select(ocursor)
	controller.inserttransferable(trans1)	
	hr:
	doc.unlockcontrollers
end sub


Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2784
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] Moving Paragraphs in Writer

Post by JeJe »

Here's a general solution for moving the selection to a textrange. You could use a searchdescriptor or other method to get the position where you want to do the move to. I've included a sub that calls it for doing a move a specified number of paragraphs.

Code: Select all

REM  *****  BASIC  *****

Sub Main
moveSelectionByParagraphs -2
End Sub

'specific sub to move a cursor a number of paragraphs from the selection and call the general sub below to do the move
sub moveSelectionByParagraphs(noparas) 
	doc = thiscomponent
	controller = doc.currentcontroller
	ovc=controller.viewcursor
	oCursor = doc.gettext.createTextCursorByRange(ovc)
	
	with oCursor
	if noparas <0 then .collapsetostart else .collapsetoend

	for i = 1 to abs(noparas)
		if noparas <0  then
			succeed =.gotoPreviousParagraph(false)
		else
			succeed = .gotoNextParagraph(false)
		end if
		if succeed = false then exit for
	next
	end with
	moveSelectionToTextRange(controller,ocursor)

end sub

'general sub to move the selection in a controller to a textrange
sub moveSelectionToTextRange(controller,ocursor) 
	with controller
	ovc= .viewcursor
	if ovc.string ="" then exit sub
	
	on error goto hr
	.model.lockcontrollers	
	trans1= .gettransferable
	ovc.string = ""

	.select(ocursor)
	.inserttransferable(trans1)	
	hr:
	.model.unlockcontrollers
	end with
end sub


Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply