【发布时间】:2022-07-20 02:54:45
【问题描述】:
我是一个相对新手的 JS 开发人员,我正在使用 Visual Studio 2019 开发一个 Word 插件。我使用以下代码将 XML 插入到活动文档中,目的是向文件。
function addCustomStyle() {
let myOOXMLRequest = new XMLHttpRequest();
var myXML;
myOOXMLRequest.open("GET", "./CustomStyle.xml", false);
myOOXMLRequest.send();
if (myOOXMLRequest.status === 200) {
myXML = myOOXMLRequest.responseText;
}
Office.context.document.setSelectedDataAsync(
myXML,
{ coercionType: Office.CoercionType.Ooxml},
function (asyncResult) {
let error = asyncResult.error;
if (asyncResult.status == Office.AsyncResultStatus.Failed) {
console.log(error.name + ": " + error.message);
}
else {
console.log("XML injection success");
}
});
}
我得到“数据写入错误:无法写入当前选择。:指定数据对象的格式无效。”在我的 CustomStyle.xml 文件中包含以下数据:
<pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage">
<pkg:part pkg:name="/word/styles.xml"
pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml">
<pkg:xmlData>
<w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" >
<w:style w:type="character" w:customStyle="1" w:styleId="tester">
<w:name w:val="tester"/>
<w:basedOn w:val="DefaultParagraphFont"/>
<w:uiPriority w:val="1"/>
<w:qFormat/>
<w:rsid w:val="00E82EF6"/>
</w:style>
</w:styles>
</pkg:xmlData>
</pkg:part>
</pkg:package>
欣然接受任何和所有的想法和建议。
干杯。
提姆
【问题讨论】:
-
来自javascript tag info:“[JavaScript] 与 Java 编程语言无关,只有表面上的相似之处。...”
-
感谢您更正我的标记。非常感谢。
标签: ms-word styles office-js office-addins word-addins