0

I need some help creating a libreoffice writer macro.

my goal is:

  1. the cursor (the blinking one) is somewhere located in the text body.
  2. the next footnote anchor contained in the text body, which is (optically) following up after the text cursor's current position, should be detected.
  3. the cursor should be moved to the end of the footnote footer (aka footnote content) that corresponds with the footnote anchor detected in step (2.).

i asked chatgpt and microsoft copilot and came up with this solution, that mostly works:

Sub JumpToNextFootnoteFooter
    Dim oDoc As Object
    Dim oViewCursor As Object
    Dim oFootnotes As Object
    Dim oFootnoteAnchor As Object
    Dim i As Integer
oDoc = ThisComponent
oViewCursor = oDoc.CurrentController.ViewCursor
oFootnotes = oDoc.getFootnotes()

' Find the next footnote anchor following the cursor
For i = 0 To oFootnotes.Count - 1
    oFootnoteAnchor = oFootnotes(i).Anchor
    If oDoc.Text.compareRegionStarts(oViewCursor.Start, oFootnoteAnchor) = 1 Then
        ' Create a cursor inside the footnote
        oFootnoteCursor = oFootnotes(i).Text.createTextCursor()
        ' Move to the end of the footnote
        oFootnoteCursor.gotoEnd(False)
        ' Move the view cursor to match
        oViewCursor.gotoRange(oFootnoteCursor, False)
        Exit Sub
    End If
Next

MsgBox "No footnote found after cursor position.", 64, "Footnote Navigation"

End Sub

however, if the cursor is located immediately before the following anchor (i.e. there is no character between the cursor and the anchor), the cursor is moved to the end of the footnote that corresponds to the following [anchor + 1] (so the desired anchor is basically skipped).

i have a strong suspicion that this is because of the fact, that a cursor at such position is technically not before the "following" anchor, but rather at the very same place as the "following" anchor. (i.e. the cursor is only optically before the anchor, but logically cursor and anchor are at the same place).

unfortunately my coding skills are insufficient and i couldn't get chatgpt or copilot to mitigate this issue.

thank you for your help!

Keltari
  • 75,447
LizLee
  • 43

1 Answers1

1

got i by myself:

instead of If oDoc.Text.compareRegionStarts(oViewCursor.Start, oFootnoteAnchor) = 1

the correct solutions is

If oDoc.Text.compareRegionStarts(oViewCursor.Start, oFootnoteAnchor) > -1

because that includes 0, which is the result when cursor and anchor are at the same position.

LizLee
  • 43