I am creating a Word add-in that writes in a document while generating some text.
The problem is that sometimes the document already has something above the generated text, so I need to keep that while removing the generated text because I need to process that text.
So, at the end of the text generation, I need to remove the written text and replace it with the processed one (the text is written while being generated, but it’s not formatted correctly, it gets formatted at the end of generation).
How do I do it without having to body.clear() the whole document? I was thinking of putting the text between some tags, like:
STARTOF
text
here…
ENDOF
and removing everything between these tags, but how would I do it?
async function convertWordToHtml(finalText: string) {
await window.Word.run(async (context: any) => {
const body = context.document.body;
body.font.name = "Open Sans";
body.font.size = 11;
body.lineSpacing = 1.5;
const converter = new showdown.Converter({ simpleLineBreaks: true, noHeaderId: true });
const html = converter.makeHtml(finalText);
// here lies the problem, it removes everything of the body that is not in the finalText (generated text)
// I can't just insert the text because the finalText is written on the document on the fly
// It enters this function only when it's finished
body.clear();
body.insertHtml(html, "end");
await context.sync();
});
}