Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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#.

like image 880
mainak chakraborty Avatar asked Jul 27 '10 05:07

mainak chakraborty


2 Answers

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
}
like image 65
Mikael Svenson Avatar answered Nov 17 '22 00:11

Mikael Svenson


This is how You avoid using localized style name:

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

}
like image 37
Pavel Avatar answered Nov 17 '22 00:11

Pavel