1

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();
    });
}
6
  • 1
    Please clarify what you mean by "writes in a document while generating some text" and "the text is written while being generated". Commented Apr 30, 2024 at 20:22
  • @RickKirkham basically, I use an AI service that sends me pieces of text, on markdown format. I write it on the document, piece by piece, for the user to see what's being generated. But, at the end, I need to convert that text to html format, so it can be interpreted by word, as formatted text. I save this text on a variable, then converts it to html at the end of generation. The problem is, that I have to remove that markdown text that I writed, without having to clear the whole document Commented May 1, 2024 at 21:03
  • 1
    How much text is generated? Is it multiple paragraphs? Does the generated text include formatting? Is the generated text always inserted in the same relative location, such as always at the end of the document? Do the end users modify the generated text before it is converted to HTML? Commented May 2, 2024 at 16:32
  • @RickKirkham A lot of text is generated, like 10+ multiline paragraphs (I tried replacing the old text, but it's longer that word can accept). The text that is written on the go does not have any kind of formatting, the text that will replace that is formatted. The generated text is always added at the end of the document. The user's can write on the document, but this will not modify the text that will be converted to HTML, because I store it on a separate variable to convert it later. Commented May 3, 2024 at 19:02
  • 1
    OK. This information helps. Does your add-in "know" (or can programmatically find out) at runtime how many generated paragraphs were inserted? If so, I think that when you're ready to delete, get a reference to the last paragraph in the doc. Then use the Range.compareLocation method with the LocationRelation.adjacentBefore parameter to get each preceding paragraph that was generated. Then just range.delete() each. Commented May 4, 2024 at 21:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.