[Writer] Search a change of keyword and execute

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
AXINSI
Posts: 2
Joined: Wed Jul 11, 2018 4:04 pm

[Writer] Search a change of keyword and execute

Post by AXINSI »

Hello everyone,

I am looking without any breakthrough so far to make Macro in Writer.
I have unfortunately almost no knowledge of OpenOffice Basic and its API. Thus I need your expertise and your generosity.
So I was planning to reach my ends with the Macro-recorder. I was on the verge of success if it were not because of the limitation of the recorder. My macro must search the first iteration of a key word and all the first iteration of its variation. Unfortunately the search-engine used with the recorder (same as CTRL+F) makes a pop-up each time one of the iterations is not in the document. So I have more or less 30 pop-up each time the macro runs which way too much. So according to my research, it is required to use a search.descriptor. Which is good because I didn't know what a search.descriptor was before reading about it.

I would have take the time learn the OpenOffice Basic but unfortunately I have professional urges for this macro. It is not a question of hours or days, but I don't have time for weeks, I'm afraid.

For those who only want the essential, my macro needs to do this steps :
- Select the first 6 lines of my documents
- copy them
- then detect each time my key-word change. For example I have an array which goes from J01 to J60 and also EUJ01 to EUJ60, and I need to act only when it changes from one to another (FindFirst).
- When it detects a change -> then it must go to the line just above
- Insert a page break
- Past the copied text (first step)
It must does all of that in a loop until it has checked every keywords.

For those who needs context like me : I want my macro to detect when files in my list change of category/keyword. And only when it changes, because there are several files with the same keyword. When it detects this change, it must insert a page-break just above the first file with the new keyword and copy a short text. The list is automatically generated through an export from my company software (ERP). In one list the keyword can change up to thirty times and several lists are generated each day. You understand the need to automate it.

From what I could understand so far about the BASIC I need a search.descriptor that search for an array of my keywords with the condition FindFirst. Then if it detects the first iteration of a keyword it must execute my manipulation and all of that in a loop.
But with my limited time and knowledge I can neither conceive a working search.descriptor nor the code for the manipulation. My knowledge is limited to the Macro-recorder.

I have no idea of the amount of work and feasability it represents. I would be in your debt, if you could help me.

Here is an exemple of the macro I made with the recorder. I had to copy most of the macro for each keyword...

Code: Select all

sub JI
rem ----------------------------------------------------------------------
rem define variables
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")

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:GoToStartOfDoc", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:EndOfParaSel", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:EndOfParaSel", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:EndOfParaSel", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:EndOfParaSel", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:EndOfParaSel", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:Copy", "", 0, Array())

rem J01
dim args9(18) as new com.sun.star.beans.PropertyValue
args9(10).Name = "SearchItem.SearchString"
args9(10).Value = "J01"

dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args9())

rem ----------------------------------------------------------------------
dim args10(1) as new com.sun.star.beans.PropertyValue
args10(0).Name = "Count"
args10(0).Value = 1
args10(1).Name = "Select"
args10(1).Value = false

dispatcher.executeDispatch(document, ".uno:GoUp", "", 0, args10())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:InsertPagebreak", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:Paste", "", 0, Array())

rem J02 and repeated for each iteration because I don't how to search an array with the recorder.
dim args11(18) as new com.sun.star.beans.PropertyValue
args11(10).Name = "SearchItem.SearchString"
args11(10).Value = "J02"

dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args11())

rem ----------------------------------------------------------------------
dim args10(1) as new com.sun.star.beans.PropertyValue
args12(0).Name = "Count"
args12(0).Value = 1
args12(1).Name = "Select"
args12(1).Value = false

dispatcher.executeDispatch(document, ".uno:GoUp", "", 0, args12())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:InsertPagebreak", "", 0, Array())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:Paste", "", 0, Array())


end sub
OpenOffice 2.4.1 and 4.1.3 on Windows 10
User avatar
Zizi64
Volunteer
Posts: 11359
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: [Writer] Search a change of keyword and execute

Post by Zizi64 »

- Select the first 6 lines of my documents
Do you want work with the first 6 LINES or the first 6 PARAGRAPHS?

Please upload your ODF type sample document here - with your embedded macro code and with some comments.
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.
AXINSI
Posts: 2
Joined: Wed Jul 11, 2018 4:04 pm

Re: [Writer] Search a change of keyword and execute

Post by AXINSI »

- First 6 lines

- Here is an example of the generated document + a sample of the macro made through the recorder.
https://we.tl/aT7wj9phTK (Wetransfer link)

Comments are in the macro to describe my steps. Here the codes search for 3 key-words (2 of them exists and the third doesn't -> irratating box message).

Cherry on the cake : Ideally the page break would not occur for the first result (as you execute the macro you can see that it creates an almost blank page for the first page).
OpenOffice 2.4.1 and 4.1.3 on Windows 10
Post Reply