writer:macro search in selection
Greetings to all, I'm new to the forum , sorry for my poor english. I'm designing a macro to search for text in writers but only within a selected text The purpose is to search for a text, select the instances found, perform a new search on the new selected text to perform nested searches
From the menu it is sufficient to activate the "search only in selection" checkbox.
Using the macro recorder the output is:
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService ("com.sun.star.frame.DispatchHelper")
dim args1 (21) as new com.sun.star.beans.PropertyValue
args1 (0) .Name = "SearchItem.StyleFamily"
args1 (0) .Value = 2
args1 (1) .Name = "SearchItem.CellType"
args1 (1) .Value = 0
args1 (2) .Name = "SearchItem.RowDirection"
args1 (2) .Value = true
args1 (3) .Name = "SearchItem.AllTables"
args1 (3) .Value = false
args1 (4) .Name = "SearchItem.SearchFiltered"
args1 (4) .Value = false
........
........
args1 (19) .Value = false
args1 (20) .Name = "SearchItem.AlgorithmType2"
args1 (20) .Value = 1
args1 (21) .Name = "Quiet"
args1 (21) .Value = true
dispatcher.executeDispatch (document, ".uno: ExecuteSearch", "", 0, args1 ())
The property involved should be SearchFiltered or something similar, but I can't find any documentation about it.
Furthermore, SearchFiltered does not appear in the properties of the createSearchDescriptor object.
I tried to make the following macro run on the current document with a selected text segment:
oDoc = ThisComponent
oDesc = oDoc.createSearchDescriptor ()
oDesc.searchWords = TRUE
oDesc.SearchString = "little"
Dim q (0) as new com.sun.star.beans.PropertyValue
q (0) .Name = "SearchFiltered"
q (0) .Value = true
oDesc.SetSearchAttributes (q ())
oFound = oDoc.findAll (oDesc)
The oDesc.SetSearchAttributes (q ()) statement causes the runtime error due to an exception with the unexplained message Type: com.sun.star.beans.UnknownPropertyException Message:.
I searched for a long time on the net but I didn't find any examples, i don't understand how to search for text in a selection with a macro, while I didn't have any problem searching for texts and styles with macros
How to solve the problem?
Why not search directly?, why nested?
I have to search for text contained in the headins. If I look for the paragraphs associated with a given style (for example heading 3) I cannot search for text at the same time.
The text searched is the name of the style, not the textual search key
If it were possible to select only the headings with a macro, it is very fast by selecting all the instances with
and then perform the search only on the selection (a very small text segment) of the only text search key Using a macro already prepared to search in the heading (or in paragraphs with well-defined styles), just pass the text key.
Moreover it seems strange to me that by means of macros it is possible to define through the properties substantially all the values of the attributes but not that ...(more)
https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=71164
I'm new to the forum, but I've been following Mike's and Villeroy's posts for a long time, I know their availability and knowledge well. The whole community helped me find many solutions since, a few months ago, I started working with OO and LO macros, but their answers were among the most important. I take advantage of Mike's reply referring to Villeroy to thank both of them for all the help they gave me together with the community. In practice, I use writers as a simplified database to perform queries and subqueries on text content to avoid involving a database For this I need to automate the nesting of searches. I am considering a possible alternative route, if it works I will propose it eventually in the answers, I hope soon. Thanks again to everyone
If it can be useful, I found this article very recent on this topic, although it seems to refer only to calc
https://thebiasplanet.blogspot.com/20...
The 2048 value (the one of the two used in the question I linked to) is used to mean "selection only". It is used in Writer's code as well. So I suppose that you should just use that value in your "SearchItem.SearchFlags" value.
Your hypothesis seems to be working !!! . A macro containing
registered by activating the findAll button selects all instances of "structure" but only in the selected text .
The indication that gave the article on https://thebiasplanet.blogspot.com : "The sum of these flags: 16 -> search f ...... 2048 -> search only the selected cells, 4096 -> seems not be used, ..... etc "therefore does not apply only to calc, but as you would have hypothesized also in writer.
I haven't hypothesized; I had read the code before passing you the link where the question stated clearly how to make it search in selection.
Yes, from your last answer I understood only now that your phrase "the one of the two used" meant 6144 = 2048 + 4096, and therefore why I didn't see 2048 in the post of your link
Now I think that it is possible to obtain the nested search that I wanted to get.
If each instance of "structure" identifies a text area (paragraph group, section, etc.) on which to refine the search, starting from the textRange of the instances rendered by thisComponent.currentSelection you can derive from these the new search areas to be selected that contain these instances and on these perform new searches and so on until the required data is found.
Very thanks!
The other more cumbersome solution that I was considering for to use SearchDescriptor (more flexible) consisted in filtering with a macro all the text defined by particular styles (es heading) in which to perform the search, select it, temporarily modify an attribute with the macro (for example fontfamily attribute) using a font not used in the rest of the document, use searchDescriptor to search for the requested text in the only areas formatted with this new font-family. After completing searches, the initial formatting was restored
SearchDescriptor allows you to find a string in a text formatted with specific attributes, but not search in texts with a given style, because the search string is the name of the style, not the text to be found.
One last important question, if possible: which parameter of the array arguments passed to the dispatcher (or other parameter) distinguishes findAll from findFirst? The macro recorder ...(more)