How to create macro to insert comment on / footnote after all examples of search string

Hi all, I’m new to LibreOffice Basic: many thanks in advance for your help!

In VBA I am able to search for all instances of a search string in a document, and insert a comment bubble on (or alternatively a footnote after) each such instance, e.g. inserting a comment “Hello World” on each instance of “Hello” in a document using this simple code:

Sub HelloWorld()
Dim range As range
Set range = ActiveDocument.Content
Do While range.Find.Execute(“Hello”, False) = True
ActiveDocument.Comments.Add range, “Hello World”
Loop
End Sub

I see that LibreOffice Basic supports Do Loops, and I also like that (unlike VBA) LibreOffice Basic appears to support search strings using characters outside ASCII. However my attempts to reproduce a similar macro in LibreOffice Basic to the above VBA code (so that it works in LibreOffice Writer) have failed. Any help from the experts on this forum would be greatly appreciated, thanks!

You try:

Sub main()
	doc = ThisComponent
	search = doc.createSearchDescriptor()
	search.searchString = "Hello"
	ranges = doc.findAll(search)
	
	For i = 0 To ranges.Count - 1
		note = doc.createInstance("com.sun.star.text.textfield.Annotation")
		note.Content = "Hello World"
		range = ranges.getByIndex(i)
		cursor = range.Text.createTextCursorByRange(range.End)
		range.Text.insertTextContent(cursor, note, False)
	Next	
End Sub

That worked perfectly, thank you very much!