【问题标题】:Is it possible to add a custom style to a Word document using a Java Script add-in?是否可以使用 Java Script 插件向 Word 文档添加自定义样式?
【发布时间】: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


【解决方案1】:

你解决了这个问题蒂姆吗?我也在看这个。

谢谢! 柯蒂斯

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-11
    • 1970-01-01
    • 2012-05-05
    • 2013-05-15
    • 1970-01-01
    相关资源
    最近更新 更多