Macro for selecting a Section

I pretend to create a button to jump to a specific (say “carta 1”) section of a Write document. Preferably also to select the entire section for printing purposes.
I came across with this macro code, but the button does nothing. No error shown in building up the macro.
Any help would be much appreciated.
Thanks in advance and a beautiful year.

Here it is the code.

Sub JumpToAndSelectSection
    Dim oDoc As Object
    Dim oTextSections As Object
    Dim oTextSection As Object
    Dim sectionName As String
    Dim i As Integer
    
    ' Get the document
    oDoc = ThisComponent
    
    ' Specify the name of the section you want to find
    sectionName = "Carta 1"
    
    ' Get the collection of text sections in the document
    oTextSections = oDoc.getTextSections()
    
    ' Loop through the sections to find the desired one
    For i = 0 To oTextSections.getCount() - 1
        oTextSection = oTextSections.getByIndex(i)
        If oTextSection.getName() = sectionName Then
            ' Found the desired section, jump to the start and extend the selection to the end
            oDoc.getCurrentController().select(oTextSection)
            
            ' Exit the loop
            Exit For
        End If
    Next i
End Sub

Please upload an ODF type sample file too.

Thank you. Could you please be more specific of what you need. I am a newbie!

A file with two named sections inside; an inserted Form control Button (what you want to push by a mouse click), and the embedded macro code. (…We can embed the macro code listed above.) Then we will examine what is the problem in your file.

The select method requires a text range as an argument, you’re passing the section.
You need to do what you say in the comment.

' jump to the start and extend the selection to the end
cursor oDoc.Text.createTextCursor()
cursor.gotoRange(oTextSection.Anchor.Start, False)
cursor.gotoRange(oTextSection.Anchor.End, True)

oDoc.getCurrentController().select(cursor)

Thank you. I have replaced

' Found the desired section, jump to the start and extend the selection to the end
            oDoc.getCurrentController().select(oTextSection)

by

’ Found the desired section, jump to the start and extend the selection to the end
cursor oDoc.Text.createTextCursor()
cursor.gotoRange(oTextSection.Anchor.Start, False)
cursor.gotoRange(oTextSection.Anchor.End, True)

   oDoc.getCurrentController().select(cursor)

This led to the following error: Symbol cursor already defined differently.

A sample document file, please…

Hi,
Here it is hopefully what you are looking for.

Sample_askLO.odt (14.9 KB)
My objective is that through the button I either:

  1. Jump to the Section Fadas_Menina, select the entire section and print it

  2. Jump to the Section Fadas_Menina and select the entire section. From here I can then print

The idea is to add more sections below that one and create more buttons to print or select them for printing.

Option one is preferred, but may not be feasible.

Many thanks for your time.

It works for me: Missed a “=” character.

Sample_askLO_Fixed.odt (22.5 KB)

Thank you so much. I wish you a Fantastic Year.