Description
Provide required information needed to triage your issue
We are implementing an automatic text update program in Word AddIn.
We need to update the document with take care of the tracked change histories.
To do this, we need to retrieve the TrackedChange object and its surrounding text,
but we are having trouble because the retrieved text is different between Native Word and Word Online.
Your Environment
- Platform [PC desktop, Mac, iOS, Office on the web]: PC desktop, Office on the web
- Host [Excel, Word, PowerPoint, etc.]: Word
- Office version number: Version 2502 (build 18516.20000)
- Operating System: Windows and Web
- Browser (if using Office on the web): Chrome 132.0.6834.112
Expected behavior
- Getting track change object.
- Get left range, same range and right range from track change object.
- Get each texts of those ranges.
Native Word and Word Online should return same results.
By the live example:
rangeTexts[0]: left:[LEFT ] mid:[updated] mid:[center RIGHT]
rangeTexts[1]: left:[LEFT updated] mid:[center] mid:[ RIGHT]
Current behavior
Word Online: Left range and right range contains the text of tracked change itself.
Native Word: Left range and right range does not contain the text of tracked change itself.
Word Online by the live example:
rangeTexts[0]: left:[LEFT center] mid:[center] mid:[centerupdated RIGHT]
rangeTexts[1]: left:[LEFT centerupdated] mid:[updated] mid:[updated RIGHT]
Steps to reproduce
- run live examples.
Link to live example(s)
$("#run").on("click", () => tryCatch(run));
async function run() {
await Word.run(async context => {
// tracking mode off
context.document.changeTrackingMode = Word.ChangeTrackingMode.off;
await context.sync();
console.log('tracking mode: off');
// clear paragraph
context.document.body.paragraphs.getFirst().delete();
await context.sync();
console.log('clear paragraph');
});
await Word.run(async context => {
// insert text
context.document.body.insertParagraph("LEFT center RIGHT", Word.InsertLocation.start);
try {
await context.sync();
} catch (error) {
console.error(`insert failed: ${JSON.stringify(error)}`);
}
console.log('insert initial text');
// tracking mode on
context.document.changeTrackingMode = Word.ChangeTrackingMode.trackAll;
await context.sync();
console.log('tracking mode: on');
// update txt
const paragraph = context.document.body.paragraphs.getFirst();
const targetText = paragraph.search('center');
const targetRange = targetText.getFirst().getRange(Word.RangeLocation.whole);
targetRange.insertText('updated', Word.InsertLocation.replace);
try {
await context.sync();
} catch (error) {
console.error(`insert failed: ${JSON.stringify(error)}`);
}
console.log('update text');
// get track change object
const changes = paragraph.getTrackedChanges();
changes.load('text,type');
await context.sync();
changes.items.forEach((change, index) => {
console.log(`change[${index}]: text:${change.text} type:${change.type}`);
});
// get range of tarck change object
const rangesList = changes.items.map(change => {
const leftRange = paragraph.getRange(Word.RangeLocation.start).expandTo(change.getRange(Word.RangeLocation.start));
const midRange = change.getRange(Word.RangeLocation.whole);
const rightRange = paragraph.getRange(Word.RangeLocation.end).expandTo(change.getRange(Word.RangeLocation.end));
leftRange.load('text');
midRange.load('text');
rightRange.load('text');
return [leftRange, midRange, rightRange];
});
await context.sync();
rangesList.forEach((ranges, index) => {
console.log(`rangeTexts[${index}]: left:[${ranges[0].text}] mid:[${ranges[1].text}] mid:[${ranges[2].text}]`)
});
});
}
// Default helper for invoking an action and handling errors.
async function tryCatch(callback) {
try {
await callback();
} catch (error) {
// Note: In a production add-in, you'd want to notify the user through your add-in's UI.
console.error(error);
}
}
Provide additional details
Context
We are implementing an automatic text update program (sort of proofreading) in Word AddIn.
We need to update the document with take care of the tracked change histories.
To do this, we need to retrieve the TrackedChange object and its surrounding text,
but we are having trouble because the retrieved text is different between Native Word and Word Online.
e.g.
Document text is "a a a" and center "a" is deleted by tracked change.
And then our text base proofreading engine expect "a a" should be updated to "a b".
We need to skip the center 'a' (because it is deleted), and we need to update 3rd 'a' to 'b'.
So, it is important to check the tracked change text and surrounding texts.
Useful logs
- Console errors
- Screenshots
- Test file (if only happens on a particular file)
Thank you for taking the time to report an issue. Our triage team will respond to you in less than 72 hours. Normally, response time is <10 hours Monday through Friday. We do not triage on weekends.