Office js 内容控件嵌套在word中

Office js 内容控件嵌套在word中,office-js,word-addins,word-web-addins,Office Js,Word Addins,Word Web Addins,我使用Office js和word Api 1.3创建了一个项目,它将存储在数据库中的内容控件的Ooxml数据加载到文档中。 在将包含内容控件的Ooxml数据加载到文档中时,我遇到了一个问题 例如:如果我正在将3个内容控件加载到文档中 我的要求是在文档中逐个加载内容控件 但它加载第一个内容控件,在第二个内容控件内,在第三个内容控件内 用于将内容控件的Ooxml数据存储到数据库中的代码: // Create a proxy object for the ConentContro

我使用Office js和word Api 1.3创建了一个项目,它将存储在数据库中的内容控件的Ooxml数据加载到文档中。 在将包含内容控件的Ooxml数据加载到文档中时,我遇到了一个问题

例如:如果我正在将3个内容控件加载到文档中

我的要求是在文档中逐个加载内容控件

但它加载第一个内容控件,在第二个内容控件内,在第三个内容控件内

用于将内容控件的Ooxml数据存储到数据库中的代码:

        // Create a proxy object for the ConentControl body.
        var ConentControl = context.document.contentControls.getByTag('FirstControl').getFirst();

        // Queue a commmand to get the OOXML contents of the body.
        var OOXML = ConentControl.getOoxml(); 
我将这个OOXML作为一个xml字段存储在数据库中

用于检索的代码

函数GetTemplateData(){


与其直接插入内容控件,不如先尝试插入段落,然后再插入内容控件

代码 var contentControls=context.document.body.insertparagration(“text”,Word.InsertLocation.end);contentControls.insertoxml(currentOOXML,Word.InsertLocation.replace)


希望这有帮助!!!

不要直接插入内容控件,先尝试插入段落,然后再插入内容控件

代码 var contentControls=context.document.body.insertparagration(“text”,Word.InsertLocation.end);contentControls.insertoxml(currentOOXML,Word.InsertLocation.replace)


希望这有帮助!!!

您的代码正在循环中调用
上下文。通常应该避免这种情况。我不知道它是否能解决您的症状,但您能否尝试将队列
insertOoxml
行放入循环中,然后在循环完成后创建一个
上下文。sync
您的代码正在调用
context.sync
在循环中。这通常应该避免。我不知道它是否能解决您的症状,但您能否尝试在循环中插入队列
insertOoxml
行,然后在循环完成后创建一个
上下文。sync
    var apiurl = 'Service Url'


    $.ajax({
        url: apiurl,
        type: 'GET',
        dataType: 'json',
        //contentType: 'application/json',
        //data: JSON.stringify(searchDetails),
        async: false,
        success: function (data) {
            $.each(data, function (index, element) {
                var ControlName = element.ControlName;
                var ControlContent = element.ContentData;
                InsertOoxml(ControlName, ControlContent);

            });

        },
        error: function (d) {
            write("Error please try again");
        }
    });
}


function InsertOoxml(ControlName, ControlContent) {
    Word.run(function (context) {

        // Create a proxy object for the content controls collection that contains a specific tag.
        currentOOXML = ControlContent;
        if (currentOOXML != "" && currentOOXML != null) {
            // Create a proxy object for the document body.
            var DocBody = context.document.body;

            // Queue a commmand to insert OOXML in to the beginning of the body.
            DocBody.insertOoxml(currentOOXML, Word.InsertLocation.end);

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

                // Tell the user we succeeded and then clear the message after a 2 second delay
                report.innerText = "Section Loaded succeeded!";
                setTimeout(function () {
                    report.innerText = "";
                }, 2000);
            });
        }
        else {
            return context.sync().then(function () {

                // Tell the user we succeeded and then clear the message after a 2 second delay
                report.innerText = 'Data not available for the control in Database.';
                setTimeout(function () {
                    report.innerText = "";
                }, 2000);
            });
        }
    })
        .catch(function (error) {
            console.log('Error: ' + JSON.stringify(error));
            if (error instanceof OfficeExtension.Error) {
                console.log('Debug info: ' + JSON.stringify(error.debugInfo));
            }
        });
}