hdr.insertOoxml 第一次不行
hdr.insertOoxml doesn't work the first time
当我将此加载项部署到 MS Word 应用程序时,我必须多次单击该按钮才能最终成功地从 hdr.insertOoxml() 到 运行。有时不得不点击这个功能超过 4 次(见下面的代码)。虽然,这似乎在 Word 365 的网络版本中工作得更好(尽管仍然不完美)。请指教,谢谢!
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);
});
}
在 hdr.insertOOXML
的第一个和最后一个调用下方添加对 return context.sync()
的调用。
当我将此加载项部署到 MS Word 应用程序时,我必须多次单击该按钮才能最终成功地从 hdr.insertOoxml() 到 运行。有时不得不点击这个功能超过 4 次(见下面的代码)。虽然,这似乎在 Word 365 的网络版本中工作得更好(尽管仍然不完美)。请指教,谢谢!
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);
});
}
在 hdr.insertOOXML
的第一个和最后一个调用下方添加对 return context.sync()
的调用。