Search code examples
javascriptoffice-jsadd-inword-addins

Office js (Word) : The parameters of the addFromNamedItemAsync function are incorrect


I am currently on creating a Word addin and would like to create a comment on a searched word in the current word file. For inserting a binding, I call the setBinding method. I get an error telling me that the parameters of the addFromNamedItemAsync method are incorrect but I don't understand why I was checked on the doc: https://learn.microsoft.com/en-us/javascript/api/office/office.bindings?view=excel-js-preview#addFromNamedItemAsync_itemName__bindingType__options__callback_addFromNamedItemAsync_itemName__bindingType__options__callback_ but I can't find where the problem is.

My function find():

function find() {
        Word.run(function (context) {
            var searchResult = context.document.body.search('Salut', { ignorePunct: true, matchWholeWord: true });

            searchResult.load('items');

            return context.sync().then(function () {
                console.log('Found count: ' + searchResult.items.length);
                var cpt = 0;
                var id = 0;
                searchResult.items.forEach(function (range) {
                    cpt = cpt + 1;
                    id = id + 1;
                    console.log('cpt: ' + cpt);
                    const contentControlledItem = range.insertContentControl();
                    contentControlledItem.appearance = "BoundingBox";
                    contentControlledItem.title = 'titleOk'+id;

                    setBinding(id, contentControlledItem);
                });

                // Synchronize the document state by executing the queued commands,
                // and return a promise to indicate task completion.
                return context.sync();
            });
        })
    } 

My function setBindings():

function setBinding(bindingId, contentControlledItem) {
        console.log(bindingId + contentControlledItem.title);
        Office.context.document.bindings.addFromNamedItemAsync(contentControlledItem.title,   //-------
            Office.BindingType.Text,                                                          //Problem is here
            { id: bindingId },                                                                //-------
            function(result) {
                if (result.error) {
                    console.log(`add binding error: ${result.error.message}`);
            } else {
                    console.log(`Added new binding with type: ${result.value.type} and id: ${result.value.id}`);
            }
        });
    }

Thank you in advance !


Solution

  • Id must be a string :

    function find() {
            Word.run(function (context) {
                var searchResult = context.document.body.search('Salut', { ignorePunct: true, matchWholeWord: true });
    
                searchResult.load('items');
    
                return context.sync().then(function () {
                    console.log('Found count: ' + searchResult.items.length);
                    var cpt = 0;                
                    var id = 0;
                    searchResult.items.forEach(function (range) {
                        cpt = cpt + 1;
                        id = 'id'+cpt;                          //-----JUST HERE------
                        const contentControlledItem = range.insertContentControl();
                        contentControlledItem.appearance = "BoundingBox";
                        contentControlledItem.title = 'titleOk'+cpt;
                        setBinding(id, contentControlledItem);
                    });
    
                    // Synchronize the document state by executing the queued commands,
                    // and return a promise to indicate task completion.
                    return context.sync();
                });
            })
        }