如何在C#中使用Microsoft.Office.Interop.Word查找标题1下的段落?

时间:2012-06-29 07:22:24

标签: c# ms-word office-interop

我是使用Microsoft.Office.Interop.Word的学习者。考虑一下我的单词文档标题为“Header1”,并且有一些“正常”样式的句子。现在我需要找到属于“标题1”样式段落的那些行。这就是我编码的原因:

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
    }
}

如何显示这些标题下的所有行?

1 个答案:

答案 0 :(得分:1)

我想你想要这样的事情,假设我理解你的问题:

//get the 'next' paragraph but only if it exists
if (paragraph.Next() != null)
{
     MessageBox.Show("Next paragraph:" + paragraph.Next().Range.Text.ToString());
}