How to update all fields in a Word document

Matthew Fitzmaurice 0 Reputation points
2024-10-01T02:21:58.8633333+00:00

I'm new to Office Web Add-Ins (having come from VSTO land), so I'm probably not understanding something small and silly..

I have a document that contains 10 SEQ fields {{SEQ Test * MERGEFORMAT}} . I copied and pasted them, so they all show a result of '1'. I'd like to programmatically refresh them so that they show as '1' through '10'.

Can anyone please tell me what is wrong with my code? Or am I misunderstanding what the UpdateResult function should do?

export async function UpdateAllFields_Click(event: Office.AddinCommands.Event) {
  await Word.run(async (context) => {
    console.log("UpdateAllFields_Click");
    context.document.body.fields.load();
    await context.sync();
    for (let field of context.document.body.fields.items) {
      field.load(["code", "result", "locked", "type", "data", "kind"]);
      await context.sync();
      console.log(
        "Before updating:",
        "Code of first field: " + field.code,
        "Result of first field: " + JSON.stringify(field.result)
      );
      field.updateResult();
      field.select();
      await context.sync();
      field.load(["code", "result"]);
      await context.sync();
      console.log(
        "After updating:",
        "Code of first field: " + field.code,
        "Result of first field: " + JSON.stringify(field.result)
      );
    }
    await context.sync();
  });
  event.completed();
}
Word
Word
A family of Microsoft word processing software products for creating web, email, and print documents.
873 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,966 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.