I am trying to update the text in Word table cells (that are within Content Controls [CCs]). I have been doing this successfully using the Table Objects; however it is too slow for customers with large documents with many tables. So, I want to be able to use getOoxml and insertOoxml to update the table cell values (hopefully much more quickly, although I see it may be very slow in Word Online). So I would use getOoxml to get the table Ooxml within the CCs, then modify the xml programmatically, then insertOoxml back into the CCs.
I am able to get the Ooxml successfully, and programmatically modify the table Ooxml successfully using C# OpenXMLPowerTools. However, I have not been able to insertOoxml back into the CCs successfully.
If the CC was added around a selected table, then when the code runs, it always fails with "InvalidArgument". Note that when it is done this way, there is no line break above or below the table in the CC (the CC fits the table). This is how most of my customer's tables are.
If the table was added inside a CC (results in unwanted line breaks above/below the table), then the code runs, but results in adding an extra CC and a line break. So, every time the code runs the CC count increases by 1. The CCs appear to be nested inside each other. The extra CCs are not acceptable because they mess up subsequent code. It also adds an additional line break each time the code is run.
I've tried contentControls.items[0].clear(). - this removes text, but not tables or CCs, so it does not help.
Here is simplified code in Script Lab:
$("#getooxml").click(getooxml);
function getooxml() {
Word.run(function (context) {
var contentControls = context.document.contentControls;
context.load(contentControls);
return context.sync().then(function () {
var ooxml = contentControls.items[0].getOoxml(); //contains a table
return context.sync().then(function () {
contentControls.items[0].insertOoxml(ooxml.value, Word.InsertLocation.replace);
return context.sync().then(function () {
console.log('inserted OOXM. CC count:' + contentControls.items.length);
});
});
});
})
.catch(function (error) {
console.log('Error: ' + JSON.stringify(error));
if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo));
}
});
}
Are there any potential ways to make it work? For example, Is there a way I can modify the Ooxml to make it work? I suspect the reason it adds CCs is because the getOoxml() includes the parent CC itself and when inserted inside the parent CC, it adds the CC (and any nested CCs). If so, how can I remove the parent CC from the Ooxml?
I have Version 1704 (Build 8067.2115)
There is a post here indicating the added paragraph issue will be fixed next month: `context.document.body.insertOoxml` breaks documents, crashes word It also indicates there's an error where the "replace" option doesn't actually replace (but I don't think that's the case)