none
Find a string in footer of word document using interop service RRS feed

  • Question

  •            

                Word.HeaderFooter footer;
                Word.Range footerRange;

     Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
     object miss = System.Reflection.Missing.Value;
                object path = filePath;
                object readOnly = false;
      Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);

     for (int i = 1; i <= docs.Sections.Count; i++)
                    {
                        try
                        {
                            section = docs.Sections[i];
                            if (section != null)
                            {

                                footer = section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
                                footerRange = footer.Range;
    }
    }

    Need to find two string names are available in the footer. 

    Check string "Business" or "Personal" in the footer , if any of the string is available return the string.

                                
    Wednesday, September 13, 2017 4:07 PM

All replies

  • Hi usapnew,

    >>if any of the string is available return the string.

    Which string will be returned?

    You could use Indexof to compare string in footer and "Business"/"Personal".

    If compared result > 0, we could know the footer contains the string.

    Here is the example.

                            if (footerRange.Text.IndexOf("Business") > 0) {
    
                                MessageBox.Show("Business");
    
                            }
    
                            else if (footerRange.Text.IndexOf("Personal") > 0) {
    
                                MessageBox.Show("Personal");
    
                            }

    Best Regards,

    Terry

    Thursday, September 14, 2017 6:22 AM