Word Add-In quote field with spaces

Juri 0 Reputation points
2024-12-15T05:30:30.99+00:00

I am trying to add a quote field using the JS API for Word Add-ins:

export const insertQuoteField = async (str) => {
  try {
    Word.run(async (context) => {
      const range = context.document.getSelection();

      // Define the field type and field code
      const location = Word.InsertLocation.replace
      const fieldType = Word.FieldType.quote;

      // Insert the field at the current selection
      range.insertField(location, fieldType, `${str}`);
      
      // Sync the context to ensure everything is updated in the document
      await context.sync();

    });
  } catch (error) {
    console.error("Error inserting QUOTE field:", error);
  }
};

This works so far, except that if str contains spaces, the spaces are not displayed. However, if I edit the field, the spaces are there, and they appear when I click save. But I have not been able to get the spaces to display programmatically without user interaction.

Has anyone encountered this issue? Is there a way to programmatically trigger the field to update and display spaces without requiring user interaction?

User's image

Word
Word
A family of Microsoft word processing software products for creating web, email, and print documents.
901 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.
4,094 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.