Word add-in: rewriting a paragraph drops bold, italics and links inside it
paragraph.insertText(newText, Word.InsertLocation.replace) keeps the paragraph style (a heading stays a heading) but the new text is plain: bold passages, italics and hyperlinks inside the paragraph are gone. To keep them, read the inline formatting first from one body.getOoxml(), carry it through the rewrite (for example as <b>, <i>, <a href="#N"> markers for a translation model), write the plain text, then search only inside the range that insertText returned and set font.bold, font.italic and hyperlink on the found passages.
Přepis odstavce přes insertText(…, replace) nese styl odstavce, ale ne tučné, kurzívu a odkazy uvnitř. Formátování přečti z body.getOoxml(), přenes ho značkami, po zápisu hledej úseky jen ve vráceném rozsahu a nastav font.bold / font.italic / hyperlink.
Symptoms
- After a whole-document translation written paragraph by paragraph,
document.xmlshows the new text inw:inswithout anyw:band withoutw:hyperlinkor HYPERLINK fields. - The paragraph style survived, so headings look right and the loss is easy to miss.
Fix
- Read the formatting once.
context.document.body.getOoxml()returns a flat OPC package. Take the hyperlink relationships (Typeending in/hyperlink) and walk thew:bodyparagraphs in order. Skipw:delandw:moveFrom(with tracked changes your paragraph text is the "changes accepted" version) and paragraphs insidew:txbxContent, which are not inbody.paragraphs. Links come asw:hyperlink r:idand also asHYPERLINK "url"fields (w:fldSimpleorw:fldChar/w:instrText). - Pair by text, not only by position. Compare each OOXML paragraph's text with the text you read from
body.paragraphs(whitespace-normalised). If it differs, treat that paragraph as unformatted rather than guessing. - Write plain, then format the returned range.
const range = paragraph.insertText(plain, Word.InsertLocation.replace);
await context.sync();
for (const run of runs) { // { start, end, bold?, italic?, link? }
const part = plain.slice(run.start, run.end); // max 255 chars – Word search limit
const k = occurrenceIndex(plain, part, run.start); // which non-overlapping match
const hits = range.search(part.replace(/\^/g, '^^'), { matchCase: true });
hits.load('items');
found.push({ run, hits, k });
}
await context.sync();
for (const { run, hits, k } of found) {
const h = hits.items[k];
if (!h) continue; // report it, do not fail the write
if (run.bold) h.font.bold = true;
if (run.italic) h.font.italic = true;
if (run.link) h.hyperlink = run.link; // WordApi 1.3
}
await context.sync();
Search the returned range, not the paragraph. With Track Changes on, the paragraph still contains the deleted old text, and names or numbers that did not change would match there first.
What you get with Track Changes on
- New runs inside
w:inswithw:b w:val="1"/w:i w:val="1", and aw:rPrChangerecording the format change. - Links written by
range.hyperlink = urlshow up asHYPERLINK "url"fields, not asw:hyperlinkelements. - Rejecting the tracked changes written by the add-in restores the original paragraph with its original formatting.
Watch out
- Only set formatting on. Setting
font.bold = falseon the rest of the paragraph would also override bold that comes from the paragraph style (headings). - If a language model moves the markers, it can wrap a neighbouring word. Tell it that each marker must wrap exactly the translation of the text it wraps in the original, and show the user which passages get formatted before they approve.
- Keep link URLs out of the model's text (pass
#1,#2and map back). Only re-applyhttp(s):andmailto:targets.