0

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

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

4

1 回答 1

1

是的。如果您使用的是 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));
        }
    });
}

您可以从这里参考新版本的 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));
        }
    });
}
于 2016-06-23T07:34:16.260 回答