Search code examples
xmlvbams-wordms-officeoffice-interop

How to get the Paragraph style from w:sdtContent in word XML


I need to get the style of a paragraph to which i have assigned tag value ( COntentControl ). Now i have added contentControl (ie) Tag value to the paragraph in word,I can able to get the text of that corresponding paragraph to which i have assigned a tag value,

case "DocumentFormat.OpenXml.Wordprocessing.SdtBlock":
    SdtBlock p1 = (SdtBlock)DocElement;
    string content1 = p1.InnerText;

    if (!string.IsNullOrEmpty(content1))
        dt.Rows.Add(content1);
    break;

and am adding that para to the table , but i need to get the style of the para ,when i saved the word document in XML format i get these codes

    <w:sdtContent>
        <w:p w:rsidR="00D57D79" w:rsidRDefault="00176023">
            <w:pPr>
                <w:pStyle w:val="Heading1"/>
                <w:rPr>
                    <w:rFonts w:eastAsia="Times New Roman"/>
                    <w:shd w:val="clear" w:color="auto" w:fill="auto"/>
                    <w:lang w:val="en-US"/>
                </w:rPr>
            </w:pPr>
            <w:r>
                <w:rPr>
                    <w:rFonts w:eastAsia="Times New Roman"/>
                    <w:shd w:val="clear" w:color="auto" w:fill="auto"/>
                    <w:lang w:val="en-US"/>
                </w:rPr>
                <w:t>Use Case Model</w:t>
            </w:r>
        </w:p>
    </w:sdtContent>
</w:sdt>

I need to get the corresponding style of that paragraph.How to get those styles?


Solution

  •  case "DocumentFormat.OpenXml.Wordprocessing.SdtBlock":
                            SdtBlock p1 = (SdtBlock)DocElement;
                            content1 = p1.InnerText;
    
                            IEnumerable<Paragraph> pp = p1.Descendants<Paragraph>();
                            foreach (Paragraph para in pp)
                            {
                             style=   GetParagraphStyleId(para);
                            }
    

    where GetParagraphStyleId(para) has

     public string GetParagraphStyleId(Paragraph p)
        {
            string ParaStyleIdValue = string.Empty;
            if (p.ParagraphProperties != null)
            {
                if (p.ParagraphProperties.ParagraphStyleId != null)
                {
                    if (p.ParagraphProperties.ParagraphStyleId.Val != null)
                    {
                        ParaStyleIdValue = p.ParagraphProperties.ParagraphStyleId.Val.Value;
                    }
                }
            }
            return ParaStyleIdValue;
        }
    
      Using this i get the Style of the paragraph from sdtContent.