0

I wanted to use javascript addin's to get the style object and get some proerties of it for example maybe paragraph property and get the indentation of this specific style but I cant get to the style object and get the property of it. I know that there is a collection of every style in document but I cant get any properties of those. There is way to change style in selected range but it could be very fine if I could get style from the range maybe and like i said erlier get some properties of it.

This is code in Word VBA that is example of property i want to see value of:

documents("[file_name]").Styles([index]).ParagraphFormat.LeftIndent

We can set the value to any variable:

Dim var As Integer/Long
Let var = documents("[file_name]").Styles([index]).ParagraphFormat.LeftIndent
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • The next batch of updates to the Word JavaScript APIs will include a lot of enhanced support for styles. They should be available for preview soon. – Rick Kirkham Feb 13 '23 at 20:35

1 Answers1

0

As far as I know the office.js API for Word does not currently (v. 1.4) provide any Style objects with the kind of properties that you are used to seeing via the Object Model that VBA uses. All it lets you do is get the style names (localized and built-in) associated with another object such as a range, a paragraph, or various style properties associated with a table.

i.e. at the moment, the only way you would be able to get detailed style information would probably be to retrieve the document's XML and interpret that. Hard, I suspect.

I think that probably answers your question but in case you were looking for code to retrieve a style, to get the style name of a range, say, is straightforward, e.g. in Script Lab you can start with one of the basic JavaScript samples and modify its run function so it looks like this:

function run() {
  return Word.run(function(context) {
    var range = context.document.getSelection();
    range.load("style");
    
    return context.sync().then(function() {
      console.log('The selected style was "' + range.style + '".');
    });
  });
}

Precisely which style name you get depends on the range - if you select two paragraphs with different styles, the style name will be "", and so on.

jonsson
  • 655
  • 1
  • 3
  • 10
  • Thank you its really helpfull. I have one more thing bcs I am new in this and i dont really know how to get document xml and maybe edit it or include it in code and get from it any values. – ItsJust PaperMan Feb 15 '23 at 17:39
  • @ItsJustPaperMan to *get* the ooxml, perhaps start somewhere like https://learn.microsoft.com/en-us/samples/officedev/pnp-officeaddins/word-get-set-edit-ooxml/. But understanding how to interpret or use that ooxml is another thing altogether. – jonsson Feb 15 '23 at 18:33
  • Okay. I will try to get to this level. Thanks anyway you were very helpfull. – ItsJust PaperMan Feb 16 '23 at 09:23