• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Pruebas Online
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

94
Vistas
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

almost 3 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda