mainak chakraborty
mainak chakraborty

Reputation: 71

Identify heading in an ms word document using C#

I need to identify the headings and normal texts in a ms word document separately and put them in two different columns of an excel sheet. This is a VSTO application using C#.

Upvotes: 7

Views: 4403

Answers (2)

Pavel
Pavel

Reputation: 31

This is how You avoid using localized style name:

if(style.NameLocal == Doc.Styles[Word.WdBuiltinStyle.wdStyleHeading1].NameLocal){

}

Upvotes: 3

Mikael Svenson
Mikael Svenson

Reputation: 39697

Here's a short loop for the word part. Get the name of the style for a paragraph, and check it's name. The name will differ according to what is defined in your document template.

foreach (Paragraph paragraph in this.Application.ActiveDocument.Paragraphs)
{
    Style style = paragraph.get_Style() as Style;
    string styleName = style.NameLocal;
    string text = paragraph.Range.Text;
    if( styleName == "Normal" ) // do something
    else if( styleName == "Heading 1" ) // do something
}

Upvotes: 10

Related Questions