Ms word 如何使用Word javascript api为Word添加字段代码

Ms word 如何使用Word javascript api为Word添加字段代码,ms-word,office365,office-js,word-field,Ms Word,Office365,Office Js,Word Field,我想使用word JavaScript API添加字段代码。我查了一下,但没有发现任何与此相关的信息。在这个API中有没有办法做到这一点?API中没有直接支持添加字段的功能,即您必须创建包含字段的开放XML。然后,您可以在文档中插入此打开的XML(以下未经测试的代码段的一长行): //对Word对象模型运行批处理操作。 运行(函数(上下文){ //将命令排队以获取当前选择,然后 //使用结果创建代理范围对象。 var range=context.document.getSelection();

我想使用word JavaScript API添加字段代码。我查了一下,但没有发现任何与此相关的信息。在这个API中有没有办法做到这一点?

API中没有直接支持添加字段的功能,即您必须创建包含字段的开放XML。然后,您可以在文档中插入此打开的XML(以下未经测试的代码段的一长行):

//对Word对象模型运行批处理操作。
运行(函数(上下文){
//将命令排队以获取当前选择,然后
//使用结果创建代理范围对象。
var range=context.document.getSelection();
//将命令排入队列,以便在范围的开头插入OOXML。
range.InsertOxML(“
页码\*阿拉伯文\*合并格式
1.
,Word.InsertLocation.start);
//通过执行排队命令同步文档状态,
//并返回一个表示任务完成的承诺。
返回context.sync().then(函数(){
log('OOXML添加到范围的开头');
});
})
.catch(函数(错误){
log('Error:'+JSON.stringify(Error));
if(OfficeExtension.error的错误实例){
log('Debug info:'+JSON.stringify(error.debugInfo));
}
});

我测试了上面的代码片段,但没有得到任何结果。你知道吗?有没有更新字段的功能?当我插入它们时,我需要在它们显示正确的值之前更新它们(F9)。
// 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));
    }
});