Steve
Steve

Reputation: 1376

office.js replace Word table ooxml embeds the table into a new table

I have a Word table row with some cells bookmarked. I want to clone the row with similar but modified bookmarks. From what I have read I would want to use ooXml to achieve this - I don't see how to return a row or cell's range.

When trying to replace the table ooXml with the modified ooXml it embeds the table into what seems to be a new table.

I have a simple version below where I just retrieve the ooXml and insert it back and this same issue occurs:

Word.run(async context => {

  let tblRange = context.document.body.tables.getFirst().getRange();
  let tblXml = tblRange.getOoxml();

  await context.sync();

  tblRange.insertOoxml(tblXml.value, 'Replace');

  await context.sync();

});

This: enter image description here

becomes: enter image description here

is this a bug or am I doing something wrong?

Upvotes: 0

Views: 170

Answers (1)

Steve
Steve

Reputation: 1376

Explicitly deleting the range before replacing it seems to do the trick:

Word.run(async context => {

  let tblRange = context.document.body.tables.getFirst().getRange();
  let tblXml = tblRange.getOoxml();

  await context.sync();

  tblRange.delete();   // <-- Added this line
  tblRange.insertOoxml(tblXml.value, 'Replace');

  await context.sync();

});

Upvotes: 1

Related Questions