Selecting text from begining of document until cursor position

Hello,

I’d like to get the content of a document, from the begining of the document until the cursot position.

For now here’s what I have :

    ' Get cursor current position
    cursor = ThisComponent.CurrentController.getViewCursor() 
    cursorPrevPos = cursor.Start 
    
    ' Get all the text before the cursor
   ' Here I don't know how to do... 

    ' Go back to cursor position
    cursor.goToRange(cursorPrevPos, false)

As you can see, first I memorise the current cursor position because at the end I will need to place it where is initially was.

Then I need to retrieve the content of the document, from the 1st letter until the cursor position.
Not only I need to get the text, but more importantly, the fields. I actually don’t need the text but just the fields.

Then I will have to do some treatment, and at the end, the cursor will be placed where it was.

Can you tell me how I can get the content of the document between 0 to cursor position ?

Sub selectFromBeginningOfBodyTextTillHere()
On Local Error Goto failMsg
tDoc   = ThisComponent
tText   = tDoc.Text
cCtrl   = tDoc.CurrentController
cSel0   = tDoc.CurrentSelection(0)
cPos    = cSel0.Start
tCursor = tText.createTextCursorByRange(tDoc.Text.Start)
tCursor.gotoRange(cPos, True)
cCtrl.select(tCursor)
Exit Sub
failMsg:
MsgBox("Start and End of a selectable range must be inside the same text object!")
End Sub
1 Like

Thank you @Lupp . It is suppoed to select the text inside my document ?
Because I tested and it did nothing.
And also I inspected the tText variable and it seems to contain only text, and no the fields…

About fields, I tried to do :

oUserFieldMasters = ThisComponent.getTextFieldMasters()

to get all the fields that are used inside my document, but i contains ALL the fields that are available inside this document, and not only the ones that are used.

Any idea of how I can get this information ?

How should I find out anything about your issue without having access to the file you tested with?
I did my own testing, however, and the code worked as expected.

Where did you place the codeß From what obervbations did you conclude that the code “did nothing”?

I don’t understand your question about TextField objects in the context. Try to not mix up different questions.

Yes you’re right @Lupp.
I will try to attach a file containing your code.
I place my cursor somewhere where it’s yellow, and then I go to “ShowInsertFieldForm” function, and execute it.
Before, I place a breakpoint on line 22, just after the call of your method “selectFromBeginningOfBodyTextTillHere”.

I thought that the text would be selected, but it’s not the case. When the macro stops at beakpoint, if I go to see the document, nothing is selected.

But maybe I didn"'t understand ?

Tell me if I’m not clear.
Thanks for your help.

Here’s the file :
textSelection.odt (38.6 KB)

Sorry. I never use dialogs except those displayed by the UI, and I have no idea where to find the code telling me what the dialog DialogLibraries.Standard.frmInsertField is expected to do, and how it tries to do it. Probably it should find the TextField objects occurring in the CurrentSelection and prompt for a selection. For what reasons it may fail ist beyond my horizon. The selection itself is set correctly by the code I provided: From the beginning of the BodyText of the document till the cursor position inside the yellow portion. How to achieve this was the question I answered.

How to get the mentioned dialog to work as expected by the questioner, is a completely different question - and it’s not inside the range of questions I would try to answer.

I removed the dialog. Now I just run the function called AutoOpen and nothing gets selected, ever.

textSelection.odt (35.7 KB)

How should? Where is the line of code you expect a resulting selection from?
If you call correctly and run the Sub I provided, the selection is made.

If you want help with finding TextField instances inside a TextRange, post a respective new question.

This is getting a bit annoying.

I’m sorry if I “annoy” you. You don’t have to answer me if you don’t want to.

I tried to run your sub but nothing gets selected. Maybe I didn’t undertand what’s supposed to get selected, maybe you didn’t undertand what I’m trying to achieve, I don’t know.

I appreciate the time you spent to help me. But I’m sorry, things are not working as I expected. That’s why I still need some help.

Don’t answer, you already did a lot for me. Maybe someone else will be able to help me.

I’m sorry if mu question was not clear. I’m french and it’s not always easy for me to make me understand.

I apologize for that.

Please accept my apologies if you felt hurt. That was not my intention, but I wanted to be clear.

That’s exactly what you asked for by the subject of this question ( “Selecting text from begining of document until cursor position”) - and it worked every time when I tested it. i did this in a document I had prepared myself before I posted the code, and I did it in both the documents you had attached. You may try it again:
Open one of your files containing my code, set the hair cursor to a position inside the range with yellow background, and use >Tools>Macros>Run to run the macro selectFromBeginningOfBodyTextTillHere from the module modTools contained in the Standard library of your document.
The text from the beginning down to the cursor position will be selected.
This works for me with LibreOffice V 3.3 (the very first version) and with V 24.2.0.3 (the most recent released version).

Yes, you’re right, I did as you said and it’s working.

Strangely when I ran the macro inside the macro editor it was not working.
But I retried today and it’s working, even in the editor - which seems normal :slight_smile:

I’m sorry I don’t know what happened.

I have one last question if it’s ok for you : how can I retrieve the selected text and put it inside a variable ?

Thanks for your help ! And sorry for all that :wink:

For single selection for example:

s=ThisComponent.CurrentSelection.getByIndex(0).String

Maybe bad question, but do you know and use the extension XRay or MRI?

1 Like

Thanks again.
It actually works but I guess my title was not as clear as I thought :

  • Yes I wanted to select text from begenning of document until cursor position

  • But what I forgot to mention is that I wanted to retrieve it inside a variable - @KamilLanda you explained me how to do, thank you

  • Unfortunately there’s something I forgot to mention : my document contains fields, and when I get the document content inside my variable, it’ a string, and the fields don’t appear.

What I’m trying to do, is check the fields that are inside the document, just before cursor position.

So it seems that my question was not correctly formulated and I apologize…

Is there a way to retrieve all the fields that are inside the document but only between the beginning f the document, and until the cursor ?

I decided to close this topic and mark it as solved, and open a new one with a clearer question.

Thanks to all for your help !

Moved here: Getting all the fields that are inserted in a document until a certain position