Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Word: insertOoxml Not Reflecting OOXML Style Changes to Existing Styles #5491

Open
GlaserIngo opened this issue Mar 14, 2025 · 2 comments
Open
Assignees
Labels
Area: Word Issue related to Word add-ins Needs: attention 👋 Waiting on Microsoft to provide feedback Needs: author feedback Waiting for author (creator) of Issue to provide more info

Comments

@GlaserIngo
Copy link

While updates to the styles.xml package are made via insertOoxml, changes to existing styles are somehow not populated.

Your Environment

  • Platform [PC desktop, Mac, iOS, Office on the web]: Mac
  • Host [Excel, Word, PowerPoint, etc.]: Word
  • Office version number: Version 16.95 (25030928)
  • Operating System: 13.5.2 (22G91)
  • Browser (if using Office on the web): N/A

Expected behavior

When updating the OOXML representation to modify an existing style (in this case, adding <w:caps/> to the Heading1 style), the change should be applied to the document after calling insertOoxml.

Current behavior

The OOXML logged before and after the change shows the <w:caps/> element is successfully appended. However, once insertOoxml is called, the document does not reflect the style change. Notably, adding new styles works correctly via the same API. So we can be sure, that insertOoxml is actually updating the styles.xml package.

Steps to reproduce

  1. Retrieve the document’s OOXML using context.document.body.getOoxml().
  2. Parse the OOXML string into an XML document.
  3. Locate the <w:style> element for Heading1 (with w:styleId="Heading1") and append a <w:caps/> node within its <w:rPr> element.
  4. Serialize the updated XML back into a string and insert it using context.document.body.insertOoxml() with the Word.InsertLocation.replace option.
  5. Observe that despite the XML reflecting the change, the document does not display the text in all caps.

Link to live example(s)

https://gist.github.com/GlaserIngo/09a6fc38bef9cf4c25bdc9c8438c4e60

  1. On ScriptLab, for me, the logging of xmlDoc does not work properly, hence I added the logs for printing the XML as a string
  2. When developing in my environment and debugging, however, it works printing the xmlDoc.

Provide additional details

const insertCapitalIntoHeading = async() => {
    try {
      Word.run(async context => {
        const ooxml = context.document.body.getOoxml();
        await context.sync();

        // Parse the OOXML string into an XML document
        const parser = new DOMParser();
        let xmlDoc = parser.parseFromString(ooxml.value, "application/xml");

        console.log("Before Change:");
        console.log(xmlDoc);

        // Get all <w:style> elements
        let stylesSection = xmlDoc.getElementsByTagName("w:styles")[0];

        // Find the style with w:styleId="Heading1"
        let styles = stylesSection.getElementsByTagName("w:style");

        for(let i = 0; i < styles.length; ++i) {
          if(styles[i].getAttribute("w:styleId") === "Heading1") {
            console.log("Style found");
            let heading1Style = styles[i];
            // Find the <w:rPr> inside <w:style>
            let rPr = heading1Style.getElementsByTagName("w:rPr")[0];

            // Create a new <w:caps/> node
            const caps = xmlDoc.createElementNS("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w:caps");

            // Append it inside <w:rPr>
            rPr.appendChild(caps);

            console.log("After Change:");
            console.log(xmlDoc);

            context.document.body.insertOoxml(new XMLSerializer().serializeToString(xmlDoc), Word.InsertLocation.replace);

            await context.sync();
            break;
          }
        }
        const newooxml = context.document.body.getOoxml();
        await context.sync();
        console.log("Document after update:");
        console.log(new DOMParser().parseFromString(newooxml.value, "application/xml"));

      });
    } catch(error) {
      console.error(`Error while modifying xml: ${error}`);
    }
}

Note: I verified that manually unzipping a DOCX, modifying the styles.xml to include <w:caps/>, and repacking the file successfully applies the style change. This issue appears only when updating via the Office.js API. So we can be sure, the structure of my OOXML is not the issue. But if you would like to reproduce this as well do the following:

  1. Create a .docx document
  2. Navigate to the folder containing the document with the terminal
  3. Change the document extension from .docx to .zip
  4. unzip ./myDoc.zip -d myDoc
  5. Open the styles.xml
  6. Change this: <w:style w:type="paragraph" w:styleId="Heading1"> <w:name w:val="heading 1"/> <w:basedOn w:val="Standard"/> <w:next w:val="Standard"/> <w:link w:val="Heading1Char"/> <w:uiPriority w:val="9"/> <w:qFormat/> <w:rsid w:val="004F2F1C"/> <w:pPr> <w:keepNext/> <w:keepLines/> <w:spacing w:before="360" w:after="80"/> <w:outlineLvl w:val="0"/> </w:pPr> <w:rPr> <w:rFonts w:asciiTheme="majorHAnsi" w:eastAsiaTheme="majorEastAsia" w:hAnsiTheme="majorHAnsi" w:cstheme="majorBidi"/> <w:color w:val="0F4761" w:themeColor="accent1" w:themeShade="BF"/> <w:sz w:val="40"/> <w:szCs w:val="40"/> </w:rPr> </w:style>
  7. To this: <w:style w:type="paragraph" w:styleId="Heading1"> <w:name w:val="heading 1"/> <w:basedOn w:val="Standard"/> <w:next w:val="Standard"/> <w:link w:val="Heading1Char"/> <w:uiPriority w:val="9"/> <w:qFormat/> <w:rsid w:val="004F2F1C"/> <w:pPr> <w:keepNext/> <w:keepLines/> <w:spacing w:before="360" w:after="80"/> <w:outlineLvl w:val="0"/> </w:pPr> <w:rPr> <w:rFonts w:asciiTheme="majorHAnsi" w:eastAsiaTheme="majorEastAsia" w:hAnsiTheme="majorHAnsi" w:cstheme="majorBidi"/> <w:color w:val="0F4761" w:themeColor="accent1" w:themeShade="BF"/> <w:sz w:val="40"/> <w:szCs w:val="40"/> <w:caps/> </w:rPr> </w:style>
  8. cd into the directory
  9. zip -r ../myDocChanged.zip *
  10. Rename it to myDocChanged.docx
  11. Open and observe the all caps heading style

Context

I am attempting to programmatically update the OOXML of a Word document to modify an existing style (to apply all caps formatting) using Office.js. Although direct manipulation of the DOCX file (unzipping, editing, and repackaging) works correctly, the API call via insertOoxml does not reflect the style change, which disrupts my automated workflow.

Useful logs

  • Console errors: None
@microsoft-github-policy-service microsoft-github-policy-service bot added the Area: Word Issue related to Word add-ins label Mar 14, 2025
@microsoft-github-policy-service microsoft-github-policy-service bot added the Needs: attention 👋 Waiting on Microsoft to provide feedback label Mar 14, 2025
Copy link
Contributor

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

@penglongzhaochina
Copy link

Hi @GlaserIngo ,

Thank you for reporting the issue. Looks like it shows empty When I use your script to print the xml before and after on word mac. What step is wrong?

Image

@penglongzhaochina penglongzhaochina added the Needs: author feedback Waiting for author (creator) of Issue to provide more info label Mar 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Word Issue related to Word add-ins Needs: attention 👋 Waiting on Microsoft to provide feedback Needs: author feedback Waiting for author (creator) of Issue to provide more info
Projects
None yet
Development

No branches or pull requests

3 participants