[Macro] .SearchBackwards not working

I set up a simple search macro. It works as long as I set the cursor at the beginning of the document and search forward.

But if I set the cursor at document end and search backward, which is what I need to do, the search finds nothing.

In the following code, toggling the two marked sections breaks the search.

sub testtt

Dim vDescriptor, vFound, oCursor, i, j
vDescriptor = ThisComponent.createSearchDescriptor()

'rem goto doc end:  THIS IS THE PART THAT BREAKS IT
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(document, ".uno:GoToEndOfDoc", "", 0, Array())

' Set the text for which to search and other
With vDescriptor
	.SearchString = "o"
	.SearchBackwards = True ' THIS IS THE CORRESPONDING PART THAT BREAKS IT
End With

vFound = ThisComponent.findFirst(vDescriptor)

if isnull(vFound) then
	msgbox "not found", 64, "title"
else
	oCursor = ThisComponent.CurrentController.ViewCursor
	oCursor.gotoRange(vFound, false)
end if

end sub

Hi

Use a cursor to go to end then findNext, so try something like:

sub testtt

Dim vDescriptor, vFound, oCursor

vDescriptor = ThisComponent.createSearchDescriptor()

oCursor = ThisComponent.Text.createTextCursor
oCursor.gotoEnd(false)

With vDescriptor
   .SearchString = "o"
   .SearchBackwards = True 
End With

vFound = ThisComponent.findNext(oCursor.End, vDescriptor)

if isnull(vFound) then
    msgbox "not found", 64, "title"
else
    oCursor = ThisComponent.CurrentController.ViewCursor
    oCursor.gotoRange(vFound, false)
end if

end sub

Regards

Thanks, Pierre. This is working. Still a big uphill climb on the macro, but this part is functioning. Much appreciated.