如何为Word文档office.js api的“ FirstPage”插入标头?

时间:2019-02-11 13:35:44

标签: ms-word office-js

我使用Word API将标头插入Word 2016(台式机版)中的文档中。插入仅适用于主要标头。代码getHeader("firstPage")getHeader("evenPages")的运行没有错误,但没有结果。
有什么问题吗?

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

    // Create a proxy sectionsCollection object.
    var mySections = context.document.sections;

    // Queue a commmand to load the sections.
    context.load(mySections, 'body/style');

    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    return context.sync().then(function () {

        // Create a proxy object the primary header of the first section. 
        // Note that the header is a body object.
        //var myHeader = mySections.items[0].getHeader("primary");
        var myHeader = mySections.items[0].getHeader("firstpage");
        //var myHeader = mySections.items[0].getHeader("evenpages");

        // Queue a command to insert text at the end of the header.
        myHeader.insertText("This is a header.", Word.InsertLocation.end);

        // Queue a command to wrap the header in a content control.
        myHeader.insertContentControl();

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

当我尝试使用代码行的情况下

var myHeader = mySections.items[0].getHeader("firstpage");

我希望仅在第一页上显示标题。

0 个答案:

没有答案