Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find paragraphs that comes under a header 1 using Microsoft.Office.Interop.Word in C#?

I'm a learner in using Microsoft.Office.Interop.Word. Consider I have a word document with headings in style 'Header1' and some sentences of style 'normal' comes under it. Now I need to find those lines that comes under a paragraph of style 'Header 1'. This is what I have been coded so for:

foreach (Microsoft.Office.Interop.Word.Paragraph paragraph in doc.Paragraphs)
{
    Microsoft.Office.Interop.Word.Style style = paragraph.get_Style() as Microsoft.Office.Interop.Word.Style;
    string styleName = style.NameLocal;
    string text = paragraph.Range.Text;
    if (styleName == "Heading 1")
    {
        MessageBox.Show("Sent lines :" + text.ToString()); //this will show all headings
    }
}

How do I display the all lines that comes under those headings?

like image 860
Dolo Avatar asked Nov 04 '22 21:11

Dolo


1 Answers

I think you want something like this, assuming I understand your question:

//get the 'next' paragraph but only if it exists
if (paragraph.Next() != null)
{
     MessageBox.Show("Next paragraph:" + paragraph.Next().Range.Text.ToString());
}
like image 59
JohnZaj Avatar answered Nov 14 '22 09:11

JohnZaj