Macro help: Insert text run next to each Footnote Reference in body

Hello everyone,

I am trying to create a macro for the Document Editor with a specific goal:
I need to scan the entire document, find every Footnote Reference (the small superscript number in the text), and insert a text Run (like [1], [2], etc.) immediately after it.

Essentially, I want to convert the dynamic footnote reference into a static visual marker in the text, so I can export the content later with these references visible as standard text.

The problem:
I am iterating through all paragraphs and their elements. I am trying to detect the footnote reference using GetClassType() === "footnoteReference", but I am struggling to make it work reliably. The script either doesn’t find the elements, or fails to insert the new Run correctly at the specific index.

Here is the logic I am using. Could someone advise on the correct way to detect a footnoteReference object and insert a text Run immediately after it?

(function () {
    var oDocument = Api.GetDocument();
    var aParas = oDocument.GetElements(); // (I am using a recursive collector for tables/content, simplified here)
    
    var noteCounter = 1;

    for (var i = 0; i < aParas.length; i++) {
        var oPara = aParas[i];
        var nElements = oPara.GetElementsCount();

        // Loop through paragraph elements
        for (var j = 0; j < nElements; j++) {
            var el = oPara.GetElement(j);
            
            // I am trying to detect if 'el' is a footnote reference
            var sType = "";
            try { sType = el.GetClassType(); } catch(e) {}

            if (sType === "footnoteReference") {
                // GOAL: Insert a new Run "[n]" right after this element
                
                var oRun = Api.CreateRun();
                oRun.AddText("[" + noteCounter + "]");
                
                // This part fails or doesn't behave as expected:
                // How do I insert oRun at position j+1?
                oPara.AddElement(oRun, j + 1); // This method signature might be wrong
                
                noteCounter++;
                j++; // Skip the inserted element
                nElements++; // Update count
            }
        }
    }
})();

Does GetClassType() return “footnoteReference” for these objects? Or should I look for a specific property?
Any help or snippet on how to “Inject a Run after a Footnote Reference” would be greatly appreciated.

Thank you!

Hello @Salvodif

Can you share the implementation of your var aParas = oDocument.GetElements() collector? Not every item in the document content would appear as footnoteReference as an element, thus this may be a problem.

Also, just for better overall understanding of the document structure, can you provide a sample document with footnotes as a base for running the macro?

oDocument.GetElements()
it is a method from Api.GetDocument()

There is no such method as GetElements() in the ApiDocument class. Can you elaborate?