繁体   English   中英

如何获取Microsoft Word文档的HTML内容?

[英]How to get HTML content of Microsoft Word document?

我已使用以下代码来获取Microsoft Word DocumentHTML内容:

Word.run(function (context) {
                    var body = context.document.body;
                    context.load(body, 'html');
                    console.log(body.getHtml());

                    return context.sync().then(function () {
                        console.log("Content is fetched:", body.getHtml().value);
                    });
                })
                .catch(function(error){
                    // Log additional information, if applicable:
                    if (error instanceof OfficeExtension.Error) {
                        console.log(error.debugInfo);
                    }
                });

在上面的代码中, body.getHtml()打印一个对象,该对象中有我期望的内容。 为了检索该数据,我使用.value来获取实际的HTML内容,但是它给出以下错误:

在字数据获取RichApi.Error中存在错误:“结果对象的值尚未加载。读取value属性之前,请在关联的请求上下文上调用“ context.sync()”。

我已经发出了context.sync() ,但是出现了同样的错误。 这里有什么帮助吗?

这里的根本问题是,您可以通过方法调用来获取HTML,而不会像这样加载其隐式的东西,但是您要确保在获取值之前进行同步:)

这是它的工作方式,请检查以下示例:

 Word.run(function (context) { var myHTML = context.document.body.getHtml(); return context.sync() .then(function() { console.log(myHTML.value); }); }); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM