Word Javascript API insert table throws InvalidArgument exception

190 views Asked by At

I´m trying to insert a table in my word document, but every time I get a InvalidArgument exception in my console.

Here is my code:

Word.run(function (context) {
            var selectionRange = context.document.getSelection();
            var paragraph = selectionRange.insertParagraph("", Word.InsertLocation.before);

            return context.sync()
                .then(function () {
                    var table = paragraph.insertTable(3, 4, Word.InsertLocation.before, [
                        ["Column1", "Column2", "Column3", "Column4"],
                        ["test1", "test2", "test3", "test4"],
                        ["test5", "test6", "test7", "test8"]
                    ]);
                    table.styleBuiltIn = Word.Style.gridTable1Light;
                })
                .then(context.sync)
                .catch(function (error) {
                    console.log(error);
                });

        })
        .catch(function (error) {
            console.log('Error: ' + JSON.stringify(error));
            if (error instanceof OfficeExtension.Error) {
                console.log('Debug info: ' + JSON.stringify(error.debugInfo));
            }
        });

Is there anything I´m doing wrong?

2

There are 2 answers

1
Robert Wolf On BEST ANSWER

I found out the problem. insertTable is only supported in Word API Version 1.3, which is supported in Office 2019. I´m working with an Office 2016, which only works with 1.1.

Reference Link to microsoft docs: https://learn.microsoft.com/de-de/office/dev/add-ins/reference/requirement-sets/word-api-requirement-sets

0
Dhruv Rangunwala On

Try changing

var paragraph = selectionRange.insertParagraph("", Word.InsertLocation.before);

var table = paragraph.insertTable(3, 4, Word.InsertLocation.before, [
                        ["Column1", "Column2", "Column3", "Column4"],
                        ["test1", "test2", "test3", "test4"],
                        ["test5", "test6", "test7", "test8"]
                    ]);

with

var paragraph = selectionRange.insertParagraph("", "Before");

var table = paragraph.insertTable(3, 4, "Before", [
                        ["Column1", "Column2", "Column3", "Column4"],
                        ["test1", "test2", "test3", "test4"],
                        ["test5", "test6", "test7", "test8"]
                    ]);