I am writing a Word add-in using office-js that generates an index. It uses the following code to create a section break at the end of the document, and then writes the index there:
function WriteIndex() {
Word.run(function (context) {
//
var html = GenerateIndex(); // generate the index as an html string
//
if (html.length > 0) {
var body = context.document.body;
body.insertBreak(Word.BreakType.sectionNext, Word.InsertLocation.end);
body.select("End"); // put it at the end
return context.sync().then(function () {
body = context.document.body;
body.select("End"); // put it at the end
body.insertHtml(html, Word.InsertLocation.end);
return context.sync();
});
}).catch(handleError);
}
So far, so good -- it works just as I want it to. But now I want to be able to re-run the function and delete the index that was there before regenerating and rewriting it.
I think I can get the contents of the index into a range and delete it. But I can't find anywhere in the API where you can delete a section break. I found some code at this link that iterates through the document section collection and gathers the contents of all the sections, then replaces the entire contents of the document with only the sections wanted. But this approach feels risky and convoluted to me. Another approach might be to essentially navigate to the end of the document and perform a backspace operation, since that's how you delete a section break using the Word UI, but I can't find anything in the API documentation to do that either.
Is it really the case that the API allows you to create section breaks but doesn't allow you to delete them? Has anyone done anything like this before? Is there a better approach?
Thanks very much.