Want to get last value of number range field

I inserted text field for numbering via fields>variables>number range>text>Arabic. Now I want to get the last value beside it like we do with [page no./ total pages].

Thank you for helping :slight_smile:

sample doc with inserted field

sample doc shot
sample doc shot

If your goal is to get:

 stack exchange
 .#[1/3]56
 .#[2/3]why
 .#[3/3]no vertical
 sdf
 fd

This is not possible.

You can capture the last value of the Text number range at end of document into a variable, say last. But if try to insert fields like [Number range Text/Show variable last] in your list, variable last is not yet defined and displays as zero.

There is however a contorted workaround. If you accept the pain and the added difficulty to maintain your document, here is the recipe.

  • After the last use of the number range, preferentially at end of document to ease maintenance, add a hidden paragraph containing:

    • a Number range field targeting Text, forcing value to Text so that the variable is not incremented
    • select the whole field insertion and Insert>Cross-reference, Type Set Reference with name lastText

    The paragraph is hidden so that this “technical” paragraph does not print.

  • Patch your references to the Text number range to add / after them and Insert>Cross-reference, type Insert reference, selecting lastText and Insert reference to Reference to get the contents of the technical paragraph.

EDIT Sample file AskLOlastNumberRange.odt attached

To show the community your question has been answered, click the ✓ next to the correct answer, and “upvote” by clicking on the ^ arrow of any helpful answers. These are the mechanisms for communicating the quality of the Q&A on this site. Thanks!

In case you need clarification, edit your question (not an answer which is reserved for solutions) or comment the relevant answer.

BTW, please teach me how to capture the last value into a variable. If it can’t be displayed, I hope at least it can be used for something else, like for a calculation, right?
If these things involve macros then leave, it’s out of my understanding then. But I may still get a few lines of code.

To capture into a variable, insert a field type Set variable to create a variable and give it a name. In the Value box, type an expression involving other variables and numbers. In your case, the expression is simply Text.

Caution! You can use this variable only after the capture, i.e. you can’t forward reference the value.

FYI, I’ve never used a macro yet in a Writer document, even with very sophisticated layout. I can’t find my answer to a question about having both footnotes and margin notes automatically positioned without overlap. This is the most complicated layout I’ve ever made. It uses exclusively styles with a special configuration of frame styles.

Read the Write Guide for initial information and Bruce Byfield’s remarkable tutorial Designing with LibreOffice.

And practice on short test files.

OK, so last value will be captured at the end of all instances and one can’t use that value before that as it hasn’t been captured yet. But i think we must be able to pump that value above using a simple macro, though that’s not my cup of tea.

For a macro, see @Lupp’s answer.

Without a macro, see my answer above. The idea is to store the last value at end of the document and cross-reference this last value location. It automatically updates when you add or remove items (eventually with Tools>Update>Update All to force cross-reference update (no need for last value as it is always up-to-date).

It was some fun to make something impossible happen with the help of custom code. .
Of course, the code I wrote can be refined in many ways. Have a lot of fun, too.

I don’t insert the code here, but recommend to open this attachment (NumberRangeHighExamplExercise.odt) first without the permisson to execute the contained code, to check the code for the absence of malign parts, and then to reload it with permission.
Try to get the code working for you following the guide contained in the REM lines.

Having done so, you can (safely again!) load the next attachment where you find an already completed example. The demo also is containg a suggestion how to protect the needed SetVariable fields without disturbing the relevant text.
NumberRangeHighExampleDemo.odt

To be very clear: The text fields of type DefineVariable were originbally created with the value 0. The values now shown were assigned by the contrained subroutines. There was no need to find the bottommost occurrence of any NumberRange field type. If there are’nt extraordinary many types and much more instances in a document, it is, of course, simple to get the information from the Navigator.
On the other hand the supplied code can rather easily be completed to automate the needed updates even more.
The appropriate documnt events should be ‘View created’ and ‘View going to be closed’.