Insert alphabetical Index and entries using BASIC macro

I am creating a roster program that compiles data from a several Calc sheets into a printed document in Writer. The roster mentions individuals more than once in the document. (member of certain clubs & committees)
I want to create an index that lists the member’s name and each page they appear on.
I found and created the Table of Contents but I am having trouble getting the Index and entries created by the macro.
How do I create the Index at the end of the document using BASIC? (Just like the TOC is at the front.)
How do I enter the name instances as entries to the index as the document is being compiled?

ADDED DEGREE OF DIFFICULTY: While experimenting with a writer document I was able to create an index, mark entries, and update the index but the entries inside tables did not seem to appear referenced in the updated index! Thoughts?

Looks like I mostly solved it myself.
I turned on macro recording and created index entries and did the update.
Looking at the recorded macro I found that the update is clearly shown, but the creation of the individual macro entries are commented out by the recorder.
It still looks like there is a bit of a puzzle, but I have learned much more in the process.
I don’t consider this to be the complete solution. If anyone cares to comment and add information I would be grateful.

1st prototype with Insert/ Fields/ More Fields → Variables/ User Field created via macro. The Pages in Index aren’t clickable like links, but I hope I understood well you need something like this. It is without sorting of Authors and without update of Index.
Open new document and run macro ownIndex.

global gAuthors()
global const gName="author" 'name for user TextFields

Sub ownIndex
	on local error goto bug
	rem initialization of authors
	gAuthors=array("Jožin Flaška", "Řehoř Násos", "Anča Frťanová", "Péťa Motačka")
	
	dim oDoc as object, oVCur as object
	oDoc=ThisComponent
	oDoc.Text.String=""
	oVCur=oDoc.CurrentController.ViewCursor
	
	rem put text to document and authors
	oVCur.String="1st outstanding man is "
	insertUserField(0, oDoc) 'write author
	oVCur.String=String(25, chr(13)) & "he's not bad guy, only sad, says his best friend in bottle "
	insertUserField(1, oDoc)
	oVCur.String=String(25, chr(13)) & "Hi guys, have you some drink for us today? Ask " 
	insertUserField(2, oDoc)
	oVCur.String=" and "
	insertUserField(3, oDoc)
	oVCur.String=String(25, chr(13)) & "And boys looked like some stupid alcoholics and it is end of this famous story "
	insertUserField(0, oDoc)
	oVCur.String=String(1, chr(13))
	insertUserField(2, oDoc)
	
	rem create Index
	GlobalScope.BasicLibraries.LoadLibrary("ScriptForge")
	oDoc.lockControllers
	dim oMasters as object, sElem$, sName$, oField as object, i%, o as object, sIndex$, pages(), iCount%
	sIndex=String(3, chr(13)) & "---------Index with numbers of pages---------" & chr(13)
	oMasters=oDoc.getTextFieldMasters()
	for each sElem in oMasters.ElementNames
		sName="com.sun.star.text.fieldmaster.User." & gName
		if inStr(sElem, sName)>0 then 'there is author's TextField
			oField=oMasters.getByName(sElem) 'TextField with current author
			sIndex=sIndex & oField.Content & ": "
			iCount=ubound(oField.DependentTextFields)
			redim pages(iCount)
			for i=0 to iCount 'all fields of this author
				o=oField.DependentTextFields(i)
				oVCur.goToRange(o.Anchor.Start, false) 'move visible cursor to the start of TextField to get the Number of Page
				pages(i)=oVCur.Page 'page with current TextField
			next i
			
			sIndex=sIndex & join(SF_Array.sort(pages), ", ") & chr(13)
		end if
	next
	
	rem write Index
	oVCur.goToEnd(false)
	oVCur.String=sIndex
	oVCur.goToEnd(false)
	
bug:
	if oDoc.hasControllersLocked then oDoc.unlockControllers()
End Sub


Sub insertUserField(idAuthor%, oDoc as object) 'modified: A. Pitonyak macro book - chapter 14.10.2 listening 388
	dim sLead$, sName$, sTotName$, oMasters as object, oVCur as object, oText as object, oUField as object, oMasterField as object
	oText=oDoc.Text
	oVCur=oDoc.CurrentController.ViewCursor
	
	sName=gName & idAuthor
	sLead="com.sun.star.text.FieldMaster.User"
	sTotName=sLead & "." & sName 'com.sun.star.text.FieldMaster.User.author1
	oMasters=oDoc.getTextFieldMasters()
	oUField=oDoc.createInstance("com.sun.star.text.TextField.User")
	
	if oMasters.hasByName(sTotName) then
		oMasterField=oMasters.getByName(sTotName)
	else	
		oMasterField=oDoc.createInstance(sLead)
		oMasterField.Name=sName
		oMasterField.Content=gAuthors(idAuthor)
	end if
	
	oUField.attachTextFieldMaster(oMasterField)
	oText.insertTextContent(oVCur.getEnd(), oUField, False)	
	oVCur.goToEnd(false)
End Sub
1 Like

YES!
Thank you.
No, I do not need the index entries as clickable. Just for printing into a document.