OfficeDev / office-js

A repo and NPM package for Office.js, corresponding to a copy of what gets published to the official "evergreen" Office.js CDN, at https://appsforoffice.microsoft.com/lib/1/hosted/office.js.
https://learn.microsoft.com/javascript/api/overview
Other
641 stars 92 forks source link

[WordJS] field.code set throws after field.result.insertHtml() #4388

Open adomasven opened 3 weeks ago

adomasven commented 3 weeks ago

Word Online

Trying to set field code after inserting html for the field result range throws an error.

await Word.run(async (context) => {
    const selection = context.document.getSelection().getRange();
    const field = selection.insertField('Start', 'Addin');
    field.code = `ADDIN test`;
    // field.result.insertText("Test1", "Start");
    field.result.insertHtml("<b>Test1</b>", "Start");
    await context.sync();
    field.code = `ADDIN test1after`
    // Throws a GeneralException
    await context.sync();
});

If in the above code you call field.result.insertText() instead of field.result.insertHtml() no error is thrown.

microsoft-github-policy-service[bot] commented 3 weeks ago

Thank you for letting us know about this issue. We will take a look shortly. Thanks.

YijunMS commented 3 weeks ago

Hi @adomasven , thanks for reaching us, I am investigating this issue and trying to find the solution. As a workaround, you may reacquire the field object instead of working on the same one to unblock yourself.

adomasven commented 3 weeks ago

How would I reacquire the fields? This error also occurs if you acquire existing fields from context.document.body.fields, or any other collection.

YijunMS commented 3 weeks ago

@adomasven I mean acquiring a new Field object after inserting Html instead of using the origin one, however, this may be hard if you have multiple fields in the document. I am working on the fix, and I will update once it starts to deploy.

YijunMS commented 1 week ago

@adomasven The fix is under deployment now. I will update once it is ready for test.