[Solved] [Writer] Duplicate paragraphs via Basic macro

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
patel
Posts: 36
Joined: Tue Jun 19, 2012 2:48 pm

[Solved] [Writer] Duplicate paragraphs via Basic macro

Post by patel »

Hello, I would like to copy the first 3 paragraphs of a writer document at the end of the document
Last edited by patel on Sat Apr 18, 2020 8:00 am, edited 2 times in total.
OpenOffice 4.1 on Windows 10
LibreOffice 5.2 on Windows 10
JeJe
Volunteer
Posts: 2784
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Writer] Duplicate paragraphs via basic macro

Post by JeJe »

If you read up on text cursors.

https://wiki.openoffice.org/wiki/Writer/API/Text_cursor

Code: Select all

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

Sub Main
doc = thiscomponent
vc = doc.currentcontroller.viewcursor
tc = doc.text.createtextcursorbyrange(doc.text.getstart)
for i =1 to 3
tc.gotoNextParagraph(true)
next
doc.currentcontroller.select(tc)
trans = doc.currentcontroller.gettransferable
tc.gotorange(doc.text.getend,false)
doc.currentcontroller.select(tc)
doc.currentcontroller.inserttransferable(trans)
End Sub
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
patel
Posts: 36
Joined: Tue Jun 19, 2012 2:48 pm

Re: [Writer] Duplicate paragraphs via basic macro

Post by patel »

Perfect, thank you very much.
OpenOffice 4.1 on Windows 10
LibreOffice 5.2 on Windows 10
patel
Posts: 36
Joined: Tue Jun 19, 2012 2:48 pm

Re: [Writer] Duplicate paragraphs via Basic macro

Post by patel »

how can I change the code to copy paragraphs 3 to 5? or the selected paragraphs)
OpenOffice 4.1 on Windows 10
LibreOffice 5.2 on Windows 10
User avatar
Zizi64
Volunteer
Posts: 11361
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: [Writer] Duplicate paragraphs via Basic macro

Post by Zizi64 »

or the selected paragraphs)
You must study the API descriptions of the AOO/LO - and read Andrew Pitonyak"s free macro books.
API: Application Programming Interface.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
JeJe
Volunteer
Posts: 2784
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Writer] Duplicate paragraphs via Basic macro

Post by JeJe »

The line

Code: Select all

trans = doc.currentcontroller.gettransferable
copies the selected text.

Play around with the following bit and what you learn by reading the above link I posted: work through that page till you understand it then you'll be able to answer the change of paragraphs bit yourself. I'm not giving you the whole answer cause we'll be here forever with someone writing all your code for you and you never learning how to do it for yourself.

Code: Select all

for i =1 to 3
tc.gotoNextParagraph(true)
next

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
patel
Posts: 36
Joined: Tue Jun 19, 2012 2:48 pm

Re: [Writer] Duplicate paragraphs via Basic macro

Post by patel »

my solution

Code: Select all

Sub CopySelection
Doc = thiscomponent
trans = doc.currentcontroller.gettransferable
tc = doc.text.createtextcursorbyrange(doc.text.getend)
doc.currentcontroller.select(tc)
doc.currentcontroller.inserttransferable(trans)

end sub
OpenOffice 4.1 on Windows 10
LibreOffice 5.2 on Windows 10
Post Reply