none
How to read header and footer of the word document using interop services in C# RRS feed

  • Question

  • I have a word document and need to read data from that, especially Header and footer.

    Header and footer have a table with details like Document title, version, Date, page no etc in each cell. Found some code in online.

                                

    for (int i = 1; i <= docs.Sections.Count; i++)
                    {
                        try
                        {
                            section = docs.Sections[i];
                            if (section != null)
                            {
                                headOrFooter = section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
                                hasNumberPages = HeaderOrFooterHasPageNumber(headOrFooter);
                                if (hasNumberPages)
                                    break;


                                                            headOrFooter = section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages];
                                hasNumberPages = HeaderOrFooterHasPageNumber(headOrFooter);
                                if (hasNumberPages)
                                    break;

                                headOrFooter = section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage];
                                hasNumberPages = HeaderOrFooterHasPageNumber(headOrFooter);
                                if (hasNumberPages)
                                    break;

                                headOrFooter = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
                                hasNumberPages = HeaderOrFooterHasPageNumber(headOrFooter);
                                if (hasNumberPages)
                                    break;

                                headOrFooter = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages];
                                hasNumberPages = HeaderOrFooterHasPageNumber(headOrFooter);
                                if (hasNumberPages)
                                    break;

                                headOrFooter = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage];
                                hasNumberPages = HeaderOrFooterHasPageNumber(headOrFooter);
                                if (hasNumberPages)
                                    break;
                            }
    }}

    private bool HeaderOrFooterHasPageNumber(Word.HeaderFooter headOrFooter)
            {
                if (headOrFooter != null)
                    return headOrFooter.PageNumbers.Count > 0;
                else
                    return false;
            }

    But how to read the table in each header and footer?

    Regards,

    Suresh


    • Edited by usapnew Thursday, August 17, 2017 12:38 PM
    Thursday, August 17, 2017 11:50 AM

Answers

  • Hi usapnew,

    You could get range of the header/footer and then get the table from the range. Once you get the table, it's easy to get text in cells.

    Here is the example.

               

     Word.Section section;
    
                Word.HeaderFooter header;
    
                Word.HeaderFooter footer;
    
                Word.Range headerRange;
    
                Word.Range footerRange;
    
                Word.Table table;
    
                string title;
    
                string version;
    
                string date;
    
                string page;
    
     
    
                for (int i = 1; i <= docs.Sections.Count; i++)
    
                {
    
                    try
    
                    {
    
                        section = docs.Sections[i];
    
                        if (section != null)
    
                        {
    
                            header = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
    
                            headerRange = header.Range;
    
                            if (headerRange.Tables.Count > 0) {
    
                                table = headerRange.Tables[1];
    
                                title = table.Cell(1,2).Range.Text;
    
                                version = table.Cell(2, 2).Range.Text;
    
                                date = table.Cell(2, 2).Range.Text;
    
                            }
    
                            footer = section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
    
                            footerRange = footer.Range;
    
     
    
                            if (footerRange.Tables.Count > 0)
    
                            {
    
                                table = footerRange.Tables[1];
    
                                page = table.Cell(1, 2).Range.Text;                         
    
                            }
    
                        }
    
                    }
    
                    catch { }
    
                }

    Best Regards,

    Terry

    • Marked as answer by usapnew Wednesday, August 23, 2017 2:20 PM
    Friday, August 18, 2017 7:01 AM