Office Add-in development: Insert table in Word 2016

夙愿已清 提交于 2019-12-06 08:05:53

Maybe Michael is not aware of this, but we recently shipped (its now GA) a table object that you can use in word. And gives you WAY more functionalities than just inserting the HTML.

Here is the documentation for the table object: https://docs.microsoft.com/en-us/javascript/api/word/word.table?view=office-js

btw your code has an error. the expected argument is a 2D array. so you need to supply something like this:

   Word.run(function (context) {
            // Create a proxy object for the document body.
            var body = context.document.body;

            body.insertTable(2, 2, Word.InsertLocation.end, [["a","b"], ["c","d"]]);

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

            console.log(e.message);
        })
        

hope this helps!!!

thanks!! Juan (PM for the Word JavaScript API)

You can use the insertHTML method on any Range/Body/Paragraph object to accomplish this task. Here's the code:

Word.run(function (context) {
    context.document.body.insertHtml(
        "<table><tr><td>a</td><td>b</td></tr><tr><td>1</td><td>2</td></tr></table>",
        Word.InsertLocation.end
    );
    return context.sync().then(function(){});
}).catch(function(error){});

-Michael Saunders (PM for Office add-ins)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!