我们如何使用Word JavaScript API为Word添加域代码 [英] How do we add field code for Word using word javascript api

查看:150
本文介绍了我们如何使用Word JavaScript API为Word添加域代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用单词JavaScript API添加域代码.我查看了 API文档,但是我对此一无所获.有没有办法在此API中做到这一点?

I want to add field code using word JavaScript API. I checked the API documentation but I found nothing regarding this. Is there a way to do it in this API?

推荐答案

API中不直接支持添加字段,即您将必须创建包含该字段的Open XML.然后,您可以将此Open XML插入文档中(以下未经测试的代码行很长):

There is no direct support in the API for adding fields, i.e. you will have to create Open XML containing the field. You can then insert this Open XML in your document (something a long the lines of the following untested snippet):

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

    // Queue a command to get the current selection and then
    // create a proxy range object with the results.
    var range = context.document.getSelection();

    // Queue a commmand to insert OOXML in to the beginning of the range.
    range.insertOoxml("<pkg:package xmlns:pkg='http://schemas.microsoft.com/office/2006/xmlPackage'>
         <pkg:part pkg:name='/_rels/.rels' pkg:contentType='application/vnd.openxmlformats-package.relationships+xml' pkg:padding='512'>
             <pkg:xmlData>
                 <Relationships xmlns='http://schemas.openxmlformats.org/package/2006/relationships'><Relationship Id='rId1' Type='http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument' Target='word/document.xml'/></Relationships>
             </pkg:xmlData>
         </pkg:part>
         <pkg:part pkg:name='/word/document.xml' pkg:contentType='application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml'>
             <pkg:xmlData>
                 <w:document xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' >
                 <w:body>
                 <w:p w:rsidR="00000000" w:rsidRDefault="0043114D">
                     <w:r>
                        <w:fldChar w:fldCharType="begin"/>
                     </w:r>
                     <w:r>
                        <w:instrText xml:space="preserve"> PAGE  \* Arabic  \* MERGEFORMAT </w:instrText>
                     </w:r>
                     <w:r>
                        <w:fldChar w:fldCharType="separate"/>
                     </w:r>
                     <w:r>
                         <w:rPr>
                            <w:noProof/>
                         </w:rPr>
                         <w:t>1</w:t>
                     </w:r>
                     <w:r>
                         <w:fldChar w:fldCharType="end"/>
                     </w:r>
                     </w:p>
                 </w:body>
                 </w:document>
             </pkg:xmlData>
         </pkg:part>
    </pkg:package>", Word.InsertLocation.start);

    // Synchronize the document state by executing the queued commands,
    // and return a promise to indicate task completion.
    return context.sync().then(function () {
        console.log('OOXML added to the beginning of the range.');
    });
})
.catch(function (error) {
    console.log('Error: ' + JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log('Debug info: ' + JSON.stringify(error.debugInfo));
    }
});

这篇关于我们如何使用Word JavaScript API为Word添加域代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆