Search code examples
c#.netms-wordvstoword-addins

Determine if the paragraph is a standard text or a heading


Is there a way to determine if the paragraph is a standard text or a heading?

Without using any third party components like Spire.Doc

Please note the following Code: This works only if the Word Style "Heading 1" is not renamed.

object thisTempStyle = p.get_Style();
Style thisparagraphStyle = thisTempStyle as Style;
string actualStyle = thisparagraphStyle.NameLocal;

if (actualStyle == "Heading 1")
...

So, i'd like to get the Headings without knowing their names.

Thank you


Solution

  • You can also check the outline level of the paragraph (https://msdn.microsoft.com/en-us/library/office/ff839401.aspx).

    switch(thisparagraphStyle.ParagraphFormat.OutlineLevel)
    {
      case WdOutlineLevel.wdOutlineLevel1:
        // Heading 1
        break; 
      case WdOutlineLevel.wdOutlineLevelBodyText:
        // Body Paragraph
        break;
    }