4

I'm trying to set the styles of all selected paragraphs from a JavaScript addin. The code works perfectly in Word Online, but I get an error when I use it in Word Desktop. The code is as follows (yes, my test style is called "Banana", and it's big and yellow):

function updateParStyle(event) {
    Word.run(function (context) {
        var range = context.document.getSelection();
        range.load("paragraphs/items");
        return context.sync().then(function () {
            var items = range.paragraphs.items;
            // console.log(items.length + " items");
            for (var i = 0; i < items.length; i++) {
                items[i].style = "Banana";
            }
            return context.sync();
        });
    }).catch(function (e) {
        console.error(e);
        return window.open("https://urldecode.org/?text=" + JSON.stringify(e));
    });
    event.completed();
}

In desktop Word I get the following error:

{
    "name":"OfficeExtension.Error",
    "code":"ItemNotFound",
    "message":"ItemNotFound",
    "traceMessages":[],
    "debugInfo":{
        "errorLocation":"ParagraphCollection.getItem"
    },
    "stack":"ItemNotFound: ItemNotFound\n   at Anonymous function (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.debug.js:8103:6)\n   at lib$es6$promise$$internal$$tryCatch (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.debug.js:8974:8)\n   at lib$es6$promise$$internal$$invokeCallback (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.debug.js:8984:8)\n   at lib$es6$promise$$internal$$publish (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.debug.js:8960:9)\n   at lib$es6$promise$asap$$flush (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.debug.js:8779:8)"
}

Debugging has revealed that the error occurs in the context.sync after setting the paragraph styles. As can be seen in the error message I am using the unminified office.js for debugging, but the error also happens with the default office.js, just with a less helpful stacktrace. If I set range.style = "Banana" instead of working with paragraphs that does work on both Word Online and Word Desktop. The "Banana" style is a linked style (so it should work for both paragraphs and characters).

I get the exact same error when replacing items[i].style = "Banana" with items[i].delete() or items[i].insertText("Hello world", "After"), so the problem is not related to the style itself.

A possible workaround that I've found is that I can set a paragraph-style on a selected range, and it will work as expected (set the style of all selected paragraphs, even those that are partially selected), but I imagine I'll have to work with a ParagraphCollection at some point, so I'd still like to know what I'm doing wrong.

I have tested with Word versions 16.0.7341.2035 and 16.0.7167.2060.

1 Answer 1

3

Interesting. I am not sure if I would code it like that. May I suggest you to modify your code to use the paragraph collection appropriately? I think your code will be greatly simplified if you do this:

Word.run(function(context) {
     var pars = context.document.getSelection().paragraphs;
       pars.load();
        return context.sync().then(function () {
            for (var i = 0; i < pars.items.length; i++) {
                pars.items[i].style = "Banana";
            }
           
    return context.sync();
        })
}).catch(function(error) {
    console.log(error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});

this code certainly works on all platforms. thx Juan.

Sign up to request clarification or add additional context in comments.

3 Comments

I'll make sure to try your version. Could you explain the difference? The desktop word version did report the correct number of selected paragraphs in the paragraphs.items property, so I got the impression that it was initialized properly.
The long story short is that this is kind of a a bug that we need to fix. The long story goes to the basics of why Word.run exists. There used to be a time in the early stages of our API where when you needed to keep a variable between batches you needed do it in an explicit way, by adding your variables into what we call a references collection and you also needed to make sure to release resources after using them, kind of in a calloc/malloc fashion. So the beauty of Word.run is that the developer does not need worry about that complexity and just use the variables across batches, we do the
heavy lifting of keeping the references for the developer. The issue with your code is that we are not keeping the references of the collection you are asking in the load statement on windows (and in online there is an update coming that will also fail) . But as a result of your question (thx!), we are thinking about also adding the references of the items requested in the load . So for now please modify a bit your programming style as I proposed and you should be ok. In a few months your original code will also be successful everywhere. Thx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.