Ask Your Question
0

[Macro] .SearchBackwards not working

asked 2016-06-11 22:02:29 +0200

paul1149 gravatar image

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
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2016-06-12 09:05:24 +0200

pierre-yves samyn gravatar image

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

edit flag offensive delete link more

Comments

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

paul1149 gravatar imagepaul1149 ( 2016-06-12 20:44:22 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2016-06-11 22:02:29 +0200

Seen: 45 times

Last updated: Jun 12 '16