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
to instead do:
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:
(And, of course, a corresponding change to the HTML to add the two buttons):
Hope this helps,
~ Michael