Macro: Repeat Find until end of document

Update: Installed and tested in Writer with same result.
Windows 10

I have a lot of documents (chapters of a book) where normal paragraphs are styled with Text Body. I need a macro to change all of them so that the first paragraph after the first heading is set to Text Body First Para, and the same with each paragraph following a section marker (which is 5 asterisks).
The first bit is easy: move to the start of the document, move down one line, set the style.
Then I need a loop:

  1. Find “*****”
  2. If the text was found…
    2 a) Move down a line
    2 b) Change style
  3. Repeat

I recorded a macro to do that once, and then added the repeat loop by hand. I need to determine whether the Find was successful or not. Here is the relevant section of my code:

Dim res As Object
dim args4(21) as new
args4(0).Name = "SearchItem.StyleFamily"
args4(0).Value = 2
args4(1).Name = "SearchItem.CellType"
args4(1).Value = 0
args4(2).Name = "SearchItem.RowDirection"
args4(2).Value = true
args4(3).Name = "SearchItem.AllTables"
args4(3).Value = false
args4(4).Name = "SearchItem.SearchFiltered"
args4(4).Value = false
args4(5).Name = "SearchItem.Backward"
args4(5).Value = false
args4(6).Name = "SearchItem.Pattern"
args4(6).Value = false
args4(7).Name = "SearchItem.Content"
args4(7).Value = false
args4(8).Name = "SearchItem.AsianOptions"
args4(8).Value = false
args4(9).Name = "SearchItem.AlgorithmType"
args4(9).Value = 0
args4(10).Name = "SearchItem.SearchFlags"
args4(10).Value = 0
args4(11).Name = "SearchItem.SearchString"
args4(11).Value = "*****"
args4(12).Name = "SearchItem.ReplaceString"
args4(12).Value = ""
args4(13).Name = "SearchItem.Locale"
args4(13).Value = 255
args4(14).Name = "SearchItem.ChangedChars"
args4(14).Value = 2
args4(15).Name = "SearchItem.DeletedChars"
args4(15).Value = 2
args4(16).Name = "SearchItem.InsertedChars"
args4(16).Value = 2
args4(17).Name = "SearchItem.TransliterateFlags"
args4(17).Value = 256
args4(18).Name = "SearchItem.Command"
args4(18).Value = 0
args4(19).Name = "SearchItem.SearchFormatted"
args4(19).Value = false
args4(20).Name = "SearchItem.AlgorithmType2"
args4(20).Value = 1
args4(21).Name = "Quiet"
args4(21).Value = true

res = dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args4())

While res.Result = true
   dispatcher.executeDispatch(document, ".uno:GoDown", "", 0, args2())
   dispatcher.executeDispatch(document, ".uno:StyleApply", "", 0, args3())
   res = dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args4())

I used res.Result based on this: LibreOffice: DispatchResultEvent Struct Reference
The problem is that it gets stuck in an infinite loop and I have to Ctrl-Backspace to stop it.
I also tried While dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args4()) (dropping the second call to executeDispatch). Same thing.
Tried using res.State=1. Seems to return false all the time. Click DispatchResultState in the above link.

I also created a macro in Microsoft Word which worked fine there, but when I opened it in LO it wouldn’t loop even once:

    Do While Selection.Find.Execute
        Selection.MoveDown Unit:=wdLine, Count:=1
        Selection.Style = ActiveDocument.Styles("Text Body First Para")

It’s pretty irritating that LO doesn’t act like the documentation says it should. Perhaps I should try OO: This forum entry suggests it worked as recently as this March: Apache OpenOffice Community Forum - Searching command in macro need to return success - (View topic)

On a quick read: You are searching ***** but not changing this. So you have an infinitive loop. If you would edit to --* before going down and change paragraph this should stop after you processed all ***** in your text.

Usually VBA does not work with LibreOffice-Basic. The Language is the same, but the way to access the document completly different. However there is a mode for VBA-compatibility wich helps in some simple cases.

You may try that script even in LibreOffice as that script is using the UNO-Library, not VBA.
AOO and LO a mostly identical when it comes to scripting (and you avoid the new stuff added in LO)
But as you already solved your problem there should be no need to elaborate further.

Looks like I have found my own solution. You can enumerate paragraphs and assign a style by assigning the ParaStyleName property, which seems to assign the whole style. Not very intuitive! I expected to have to assign a Style object, but hey ho.
I kept my recorded macro that changed the style of the second paragraph, i.e. the one following the chapter heading, then…

Dim oEnum As Variant
Dim oNext As Variant

oEnum = ThisComponent.Text.createEnumeration
While oEnum.hasMoreElements()
	oNext = oEnum.nextElement()
	if oNext.supportsservice("") Then
		If Trim(oNext.getString())="*****" OR  Trim(oNext.getString())="" Then
			oNext = oEnum.nextElement()
		    oNext.ParaStyleName=("Text Body First Para")
End Sub