Sub Main
mySearch = thisComponent.createSearchDescriptor()
mySearch.searchString = "fish"
mySearch.searchRegularExpression = false
myResult = thisComponent.findfirst(mySearch)
if Not IsNull(myResult) then
myResult.string = "gas"
End If
End Sub
RoryOF wrote:The definitive work on OO macros is downloadable from http://www.pitonyak.org/oo.php
Note that you should forget VB, OpenOffice BASIC is dissimilar at the lower levels one needs for document manipulation.
JeJe wrote:If I'm following you then you want to replace a unique string in a document with a string from elsewhere? If that's it then you can use a search descriptor as follows - this will replace the first occurance of "fish" with "gas"
- Code: Select all Expand viewCollapse view
Sub Main
mySearch = thisComponent.createSearchDescriptor()
mySearch.searchString = "fish"
mySearch.searchRegularExpression = false
myResult = thisComponent.findfirst(mySearch)
if Not IsNull(myResult) then
myResult.string = "gas"
End If
End Sub
JeJe wrote:If you want the whole text of the document its as simple as
wholetext = ThisComponent.text.string
ThisComponent.text.string = newtext
But the result needs to be small enough to fit into a string variable. You can also enumerate the paragraphs.
Otherwise this page shows you how to use a text cursor:
https://wiki.openoffice.org/wiki/Writer/API/Text_cursor
Lupp wrote:Concerning questions talking unspecifically of text and asking for macro guidance: What is seen as text can have a complex structure. In specific there may be frames, nested frames, frames nested into table cells, (... of tables nested into frames...).
If you can assure your "text" is the body text of the document exclusively, please do so explicitly. "No tables at all" would also be helpful in many cases.
Sub EnumerateParagraphs
REM Author: Andrew Pitonyak
Dim oParEnum 'Enumerator used to enumerate the paragraphs
Dim oPar 'The enumerated paragraph
REM Enumerate the paragraphs.
REM Tables are enumerated along with paragraphs
oParEnum = ThisComponent.getText().createEnumeration()
Do While oParEnum.hasMoreElements()
oPar = oParEnum.nextElement()
REM This avoids the tables. Add an else statement if you want to
REM process the tables.
If oPar.supportsService("com.sun.star.text.Paragraph") Then
MsgBox oPar.getString(), 0, "I found a paragraph"
ElseIf oPar.supportsService("com.sun.star.text.TextTable") Then
Print "I found a TextTable"
Else
Print "What did I find?"
End If
Loop
End Sub
Lupp wrote:Do you need the lookup table for replacements inside the Writer doc? Tables are much easier handled in Calc docs.
Use a replace descriptor as already suggesed and loop through your table. The usage will need to be a bit more elaborate, however, I'm afraid. Anyway a few 100 'Replace All' for the document may be much more efficient (and less error-prone) than simulating code for all of them at a time written in Basic.
The one hardly dispensable condition is: No replacement can create a new "escape sequence" not yet finally processed.
You may find most of the needed information here:
https://api.libreoffice.org/docs/idl/re ... iptor.html
RoryOF wrote:Set the find and replace strings as a dictionary using Python, then for all words in dictionary replace the text with the definition (the replace) string. Simple to write a small routine to extend the dictionary for new words/replacements.
Sample code given in
https://stackoverflow.com/questions/20502862/python-replacing-words-in-a-string-with-entries-from-a-dictionary
https://www.daniweb.com/programming/software-development/code/216636/multiple-word-replace-in-text-python
Lupp wrote:The one hardly dispensable condition is: No replacement will create a new "escape sequence" not yet finally processed.
Users browsing this forum: No registered users and 10 guests