0
votes

Is there a way to get the table of contents of a Word document including the styles used in an add-in?

I'm able to get the full text of the document (including the headings), using console.log(context.document.body.text) but it just gives me the entire document as a string, stripping away all styles and formatting. I need to get the heading level of each heading so my add-in can check that whatever document I have open is structured correctly.

1
Do you mean "headers" - content at the top of a page... Or do you mean "headings"? I suspect the latter, but both are techical terms in Word so this is confusing... In any case Text always returns only a string - no formatting information. Formatting information is available from a Range object. You might try working with the Paragraphs collection, which can be interated so that the Style format can be queried from each paragraph's range.Cindy Meister
@CindyMeister. Please make your comment an answer. Thanks.Rick Kirkham
OK... @RickKirkhamCindy Meister

1 Answers

1
votes

Any time the content of a Word document is queried using a Text property only the string content will be returned, stripped of all formatting information.

Formatting information is available from Range objects. In this case, get the Paragraphs collection, iterate over it and check the Style properties of the Paragraph.Range that are relevant.

Alternatively, getting the OpenXML of the body will return a string with the content as Word Open XML in the OPC flat-file format. That can be "parsed" for all kinds of information.