【问题标题】:How do we add field code for Word using word javascript api我们如何使用 word javascript api 为 Word 添加字段代码
【发布时间】:2016-10-03 07:42:33
【问题描述】:

我想使用 word JavaScript API 添加字段代码。我检查了API documentation,但对此一无所获。有没有办法在这个 API 中做到这一点?

【问题讨论】:

    标签: ms-word office365 office-js word-field


    【解决方案1】:

    API 中没有直接支持添加字段,即您必须创建包含该字段的 Open XML。然后,您可以将这个 Open XML 插入到您的文档中(类似于以下未经测试的 sn-p 的行):

    // 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));
        }
    });
    

    【讨论】:

    • 我测试了上面的代码sn-p,但是一无所获。你有什么想法吗?
    • 有更新字段的功能吗?当我插入它们时,我需要在它们显示正确值之前更新它们 (F9)。
    猜你喜欢
    • 1970-01-01
    • 2017-10-01
    • 2014-02-02
    • 2014-12-02
    • 1970-01-01
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    相关资源
    最近更新 更多