Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
Is there a way of obtaining the set of character styles within a Word document using Java Script?

Based on:

List/Enumerate all Word Styles (custom & predefined) with Office.JS API

I used the following code to get the styles from a Word 2016 document:

`enter code here`Office.context.document.getSelectedDataAsync( Office.CoercionType.Ooxml, ( result ) => {
            let xml = $.parseXML(result.value);
                let styles = $(xml).find("w\\:styles");
      styles.children().each((index, style) => {
          // There must be a more elegant way of doing this:
          let styleText = style.innerHTML;
          console.log("Index = " + index + " has Style text: " + styleText);
          let searchText = 'w:name="';
                        let startPos = 0;

          do {
              startPos = styleText.indexOf(searchText);
              if (startPos != -1) {
                  startPos = startPos + searchText.length;
                  let endPos = styleText.indexOf('"', startPos + 1);
                  let myStyleName = styleText.substring(startPos, endPos);
                  console.log("Style name: " + myStyleName);
                  styleText = styleText.substr(endPos);
                                }
              }
              while (startPos != -1);
// console.log("Out of loop");
                                  });
  }
);

However, the format of the logged output written to the console seems to differ from the referenced article, so I can't get the style type (character or paragraph) without doing some guesswork. I'm guessing that this is pilot error, so can anybody spot where I'm going wrong. I'd add sample output to this post, but it then exceeds the maximum character count.

Cheers.

Tim

about 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!