hdr.insertOoxml第一次不起作用

时间:2019-08-27 00:28:49

标签: javascript openxml office-js word-addins

当我将此加载项部署到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);
            });
        }

1 个答案:

答案 0 :(得分:2)

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