Ask Your Question

Search word an select text from current position to found word via macro

asked 2019-11-11 10:38:59 +0100

BeSt gravatar image

Hi there.

I have a very special question. I recorded some macro to quick search some "marker-characters" in a very large text. This works very well.

But now I would like to add functionalyity to this macros that selects the text between the current position in the text to the found-position of the searched marker word.

Example: For the following text I use "Macro 1" to find the position of XYZ and then I want to use "Macro 2" to find ABC and at the same time select (and set italic) the text between XYZ and ABC.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean XYZ commodo ligula eget dolor ABC. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.

Is this possible? Any help?

Thank you

Regards, BeSt

edit retag flag offensive close merge delete


I think I can answer you this evening, I have to test the code first and I can't to do now

torreone gravatar imagetorreone ( 2019-11-11 13:26:28 +0100 )edit

That would be great. Thank you.

BeSt gravatar imageBeSt ( 2019-11-11 14:10:05 +0100 )edit

2 Answers

Sort by » oldest newest most voted

answered 2019-11-11 23:03:31 +0100

torreone gravatar image

updated 2019-11-11 23:11:24 +0100

I had already developed the code to extract a string between two separators, but I had underestimated your question.

I had not grasped the importance of the request to start from the current position. And this changed everything, because I realized that my code always started from the beginning of the document, not from the current position.

Fortunately, the format of the findNext libreoffice method has a parameter to solve the problem. Answering your question has also served me in the future

This test code defines two different searchDescriptor, the first to simulate the starting point (the string "qqq"), the rest of the code to find all the instances of "ABC. * XYZ with another searchDescriptor

Using the test text

XYZthis is a 1ABC test
test test test XYZthis is a test 2ABC test test test
XYZthis test test test is a 3ABCtest test test
XYZthis test test test is a 4ABCtest test test

you should only find the 2,3,4 strings but not the 1 that precedes qqq

The code is

sub test()
   d = ThisComponent 
   oS0= d.createSearchDescriptor
   fnd0 = d.findFirst(oS0)              ' fnd0 textCursor pointing to new start of search

   oS1= d.createSearchDescriptor        ' search all string between XYZ  and ABC
       oS1.SearchRegularExpression = True
       oS1.searchAll = False
    fndq = d.findNext(fnd0.end,oS1)      ' the trick, the first parameter of findNext

   Do while NOT IsNull(fndq)
       msgbox (fndq.string)
       fndq = d.findNext(fndq.end,oS1) 
 end sub

Warning: 1) this code works only with strings also consisting of several lines but belonging to the same paragraph, I don't know how to find strings that start in a paragraph and end up in another paragraph,

The search strings "XYZ(.|\n)ABC" or "XYZ(.|$)ABC" for example don't work

2) In this moment I don't know how to use the backreferences (portions of the regex search string in parentheses) to select only the text between the separators, my code for now also selects the separators. In the coming days, if you want, I'll try to solve this problem too, but if other nicks already have the solution, better

edit flag offensive delete link more

answered 2019-11-12 09:51:58 +0100

BeSt gravatar image

Hi torreone

Thank you for your hint. With your code I created this code:

Dim SearchDesc As Object
Dim Doc As Object

Doc = ThisComponent
SearchDesc = Doc.createSearchDescriptor
SearchDesc.SearchSimilarity = True
SearchDesc.SearchSimilarityAdd = 2
SearchDesc.SearchSimilarityExchange = 2
SearchDesc.SearchSimilarityRemove = 2
SearchDesc.SearchSimilarityRelax = False
Found = Doc.findFirst (SearchDesc)

Dim SearchDescRegExp As Object  
SearchDescRegExp = Doc.createSearchDescriptor
SearchDescRegExp.SearchString = "„(.)*“"
SearchDescRegExp.SearchRegularExpression = True
SearchDescRegExp.searchAll = False

Found = Doc.findNext(Found.end,SearchDescRegExp)

Do Until IsNull(Found)
    Found.CharPosture =
    Found = Doc.findNext( Found.End, SearchDescRegExp)

This is nearly what I wanted... The last "problem" is, that this code always starts at the very beginning of the document, but I want the code to start at the "current cursor position".

Is there any possibility to "create" a com::sun::star::uno::XInterface with the current cursor position to put into findNext-method?

Thank you!!!

Ragards, BeSt

edit flag offensive delete link more


If by current position you mean the viewcursor position, just avoid the initial search (not by chance I was talking about "simulating the current position" as my first search for my code and use viewcursor as start point.

It is not clear to me which separators you use in your search and why you did not use a single regex that looked for the string between the two separators in a single operation (I am convinced that the backreference can be used not to include the two separators in the search, but these days not the time to investigate the problem

I don't know how to create the interface of which you speak

torreone gravatar imagetorreone ( 2019-11-12 17:13:13 +0100 )edit

This regex pattern


detect and extract only the string between separator XYZ and ABC without include XYZ and ABC

torreone gravatar imagetorreone ( 2019-11-17 23:06:15 +0100 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2019-11-11 10:38:59 +0100

Seen: 42 times

Last updated: Nov 12