当我将此插件部署到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);
            });
        }

最佳答案

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

关于javascript - hdr.insertOoxml第一次不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57666326/

10-10 06:50