如何在 Office 365 应用程序中获取 Word 页面的内容

how can i get contents of word page in Office 365 app

我正在创建一个Office 365 word应用程序,我想在其中获取页面的所有内容(文本+图像)。目前我正在使用 getSelectedDataAsync() 函数,但在这种情况下我需要 select 所有内容。

有没有其他方法可以在不 select 的情况下获取所有内容?

是的。如果你使用的是Office 2016,Word for iPad,Word for Mac,我们可以使用下面的代码获取Word文档的文本:

 function getDataFromBody() {
    // Run a batch operation against the Word object model.
    Word.run(function (context) {

        // Create a proxy object for the document body.
        var body = context.document.body;

        body.load("text");

        // Synchronize the document state by executing the queued commands,
        // and return a promise to indicate task completion.
        return context.sync().then(function () {              
            console.log("Body txt contents: " + body.text);
        });
    })
    .catch(function (error) {
        console.log("Error: " + JSON.stringify(error));
        if (error instanceof OfficeExtension.Error) {
            console.log("Debug info: " + JSON.stringify(error.debugInfo));
        }
    });
}

您可以参考here的新版本API。

更新

获取 Word 正文内容的Html:

   function getDataFromBody() {
    // Run a batch operation against the Word object model.
    Word.run(function (context) {

        // Create a proxy object for the document body.
        var body = context.document.body;

        // Queue a commmand to get the HTML contents of the body.
        var bodyHTML = body.getHtml();

        // Synchronize the document state by executing the queued commands,
        // and return a promise to indicate task completion.
        return context.sync().then(function () {
            console.log("Body HTML contents: " + bodyHTML.value);
        });
    })
    .catch(function (error) {
        console.log("Error: " + JSON.stringify(error));
        if (error instanceof OfficeExtension.Error) {
            console.log("Debug info: " + JSON.stringify(error.debugInfo));
        }
    });
}