Unordered numbering for tags

Hello everyone !

I am looking for a way to automatically insert numbered tags in my writer documents (e.g. “TAG 1”, “TAG 2”, “TAG 3”, … “TAG 49”, “TAG 50”, …).

The tricky part is I don’t want this number to change with the insertion of new tags before as the usual behavior with numbered headings. I want the tag to be inserted and to remain the same whatever happens elsewhere in the document. So that when I have tags 1, 2, 3 ,4 already in the document and insert a fifth just after the first, I get 1, 5, 2, 3, 4 (not 1, 2, 3, 4, 5).

The reason is I am going to reference this number in another system (e.g. “See TAG 3”) , and I don’t want to update both the document and the ticket in the other system when I edit the document.

And I want LO to manage this number by itself so that I don’t have to look up in long documents for “the last number I used” to avoid re-using the same twice.

What do you think ? Macro with a counter ? (I definitely have a hard time with LO macros… :/).

Thanks for your help ! :slight_smile:

Apart from using “unique” timestamps formatted as YYMMDDHHMMSS, I don’t see simple solution. With such a tag, you can’t guess how many tickets you have but it could solve nicely your problem.

Tell if you are interested and I’ll develop in an answer.

Thanks a lot for your input, I appreciate. It would indeed solve the problem, but it would make the tag IDs very long and hard to memorize for my colleagues who will basically be doing a ctrl-f before typing the IDs and looking them up.

Just to put a cap on this post, I ended up facing my fears (XD) and wrote a macro that inserts a numbered tag [(1), (2), (3), …] at the current cursor position and stores the last used value in a custom document property, so that when I run the macro again it never reuses the same tag, keeping them unique.

SUB TAG

dim oUserProps as object 	'Custom properties
dim varNextTag as integer 	'Next tag value
dim strTag as string 		'Tag text
dim range as object			'Position where to insert the tag in the document

oUserProps = ThisComponent.getDocumentProperties().UserDefinedProperties

'--- Create custom property if it doesn't exist
If NOT oUserProps.getPropertySetInfo().hasPropertyByName("LastUsedTag") Then
	oUserProps.addProperty("LastUsedTag",, "0")
	oUserProps = ThisComponent.getDocumentProperties().UserDefinedProperties 'Reload properties with the newly added property
End If

varNextTag = CInt(oUserProps.[LastUsedTag])+1

strTag = "("&CStr(varNextTag)&")   "

range = ThisComponent.CurrentSelection.getByIndex(0)

range.String = strTag

oUserProps.[LastUsedTag] = varNextTag

End Sub

If there aren’t too many of these tags (for the navigator) you may insert the as bookmarks creating the shown text at the same time.

You may use a find tool or the navigator then alternatively.

This solution comes with a disadvantage: If you delete the bookmarks in the navigator, the text pieces will remain, but the code to find the next number can’t work then correctly.
Of course, you might combine it with your own solution of writing the last used number to the document properties.

See example: ask303787insertTags.odt