【问题标题】:hdr.insertOoxml doesn't work the first timehdr.insertOoxml 第一次不起作用
【发布时间】:2019-08-27 00:28:49
【问题描述】:

当我将此加载项部署到 MS Word 应用程序时,我必须多次单击该按钮才能最终使 hdr.insertOoxml() 成功运行。有时不得不点击这个功能超过 4 次(见下面的代码)。虽然,这似乎在 Word 365 的 Web 版本中工作得更好(尽管仍然不完美)。请指教,谢谢!

function applyletterhead() {
    Word.run(function (context) {


        var departmentLH = document.getElementById("lh-department").options[document.getElementById("lh-department").selectedIndex].value;


        var sameLHCB = document.getElementById("sameLH");
        var secondHT = document.getElementById("secondH").value;

        if (sameLHCB.checked === true) {
            toDataURL(departmentLH + '_First.png', function (dataUrl) {
                var myOOXMLRequest = new XMLHttpRequest();
                var myXML;
                myOOXMLRequest.open('GET', '_SP_letterhead_First.xml', false);
                myOOXMLRequest.send();
                if (myOOXMLRequest.status === 200) {
                    myXML = myOOXMLRequest.responseText;
                    myXML = myXML.replace('#####secondH#####', secondHT);
                    myXML = myXML.replace('#####imagepath#####', dataUrl.replace('data:image/png;base64,', ''));
                }
                let hdr = context.document.sections.getFirst().getHeader("Primary"); //returns Word.Body type
                hdr.clear();
                hdr.insertOoxml(myXML, 'Replace');
            });
        }
        else {
            toDataURL(departmentLH + '_First.png', function (dataUrl) {
                var myOOXMLRequest = new XMLHttpRequest();
                var myXML;
                myOOXMLRequest.open('GET', '_SP_letterhead_First.xml', false);
                myOOXMLRequest.send();
                if (myOOXMLRequest.status === 200) {
                    myXML = myOOXMLRequest.responseText;
                    myXML = myXML.replace('#####secondH#####', secondHT);
                    myXML = myXML.replace('#####imagepath#####', dataUrl.replace('data:image/png;base64,', ''));
                }
                let hdr = context.document.sections.getFirst().getHeader("FirstPage"); //returns Word.Body type
                hdr.clear();
                hdr.insertOoxml(myXML, 'Replace');
                console.log("First: ", myXML);
            });

            toDataURL(departmentLH + '_Primary.png', function (dataUrl) {
                var myOOXMLRequest = new XMLHttpRequest();
                var myXML;
                myOOXMLRequest.open('GET', '_SP_letterhead_Primary.xml', false);
                myOOXMLRequest.send();
                if (myOOXMLRequest.status === 200) {
                    myXML = myOOXMLRequest.responseText;
                    myXML = myXML.replace('#####secondH#####', secondHT);
                    myXML = myXML.replace('#####imagepath#####', dataUrl.replace('data:image/png;base64,', ''));
                }
                let hdr = context.document.sections.getFirst().getHeader("Primary"); //returns Word.Body type
                hdr.clear();
                hdr.insertOoxml(myXML, 'Replace');
                console.log("Primary: ", myXML);
            });
        }

【问题讨论】:

  • 没问题 :-)

标签: javascript openxml office-js word-addins


【解决方案1】:

hdr.insertOOXML 的第一个和最后一个调用下方添加一个return context.sync() 调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-16
    • 2018-10-25
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多