I am trying to first find a paragraph, and later on when a user clicks a button, do some manipulation on the paragraph.
I tried using a sample from what seems to be the defenitive book: "Building Office Addins" by Michael Zlatkovsky.
var global_paragraph = undefined;
async function analyzeDocument() {
Word.run(async function(context) {
const paragraphs = context.document.body.paragraphs;
context.load(paragraphs, 'text');
return context.sync().then(() => {
for (let i = 0; i < paragraphs.items.length; i++) {
if (/*some condition that works only once*/) {
global_paragraph = paragraphs.items[i];
global_paragraph.track();
}
};});
}).catch(handleError);
};
async function handleButtonClick() {
OfficeExtension.config.extendedErrorLogging = true;
Word.run(global_paragraph, async function(context) {
global_paragraph.load("text");
return context.sync().then(() => {
/* do something */
});
}).catch(handleError);
};
This resulted in a General Exception.
{"code":"GeneralException","message":"GeneralException","errorLocation":"Document._GetObjectByReferenceId","statement":"var v=context.root._getObjectByReferenceId(\"p!00000DB2\");","surroundingStatements":["// >>>>>","var v=context.root._getObjectByReferenceId(\"p!00000DB2\");","// <<<<<","v.load([\"text\"]);"],"fullStatements":["var v=context.root._getObjectByReferenceId(\"p!00000DB2\");","v.load([\"text\"]);"]}
I am able to reproduce your issue in Script Lab.
I believe the issue isn't with your code, but rather with the Word APIs. The good news is that there is a simple workaround, though I would encourage you to file a bug on https://github.com/officedev/office-js/issues anyway, to make sure that the product team can look into it.
The workaround is in place of
global_paragraph = paragraphs.items[i];
to instead do:
global_paragraph = paragraphs.items[i].getRange();
By calling getRange()
, it creates a new object with a a proper identity, and thus is able to track it later.
The snippet that I used within Script Lab (effectively the same as what you had) is as follows:
$("#button1").click(() => tryCatch(button1));
$("#button2").click(() => tryCatch(button2));
var global_paragraph: Word.Range;
async function button1() {
await Word.run(async function(context) {
const paragraphs = context.document.body.paragraphs;
context.load(paragraphs, "text");
return context.sync().then(() => {
for (let i = 0; i < paragraphs.items.length; i++) {
if (paragraphs.items[i].text.startsWith("Dear")) {
global_paragraph = paragraphs.items[i].getRange();
global_paragraph.track();
}
}
});
});
}
async function button2() {
OfficeExtension.config.extendedErrorLogging = true;
Word.run(global_paragraph, async function(context) {
global_paragraph.load("text");
return context.sync().then(() => {
console.log(global_paragraph.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);
}
}
(And, of course, a corresponding change to the HTML to add the two buttons):
<button id="button1" class="ms-Button">
<span class="ms-Button-label">Button1</span>
</button>
<button id="button2" class="ms-Button">
<span class="ms-Button-label">Button2</span>
</button>
Hope this helps,
~ Michael