none
Replace Fields in Headeres/Footers Word Automation C#, Interop RRS feed

  • Question

  • How to replace a specific portion of the header/Footer. If there any fields like Filename with path, Date. 

    How to Get the Fields in Header/Footer. 

    I know Field is a part of collection of Fields. Fields are part of Range. Range is a part of sections.

     foreach (Microsoft.Office.Interop.Word.Section section in wordDocument.Sections)
                        {

                           section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text;

                         }

    Foreach (Field in Fields)

    {

    }

    --- With the all above and Range.Find.Execute method also doesn't satisfy this requirement. please suggest

    Thursday, July 18, 2013 6:10 AM

Answers

  • What, exactly, do you want to do with the fields? You mention "replace" in the subject, but don't say what you want to replace them with. That makes it difficult to make any useful suggestions, other than:

    Word.Range rng = section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
    Word.Fields flds = rng.Fields;
    foreach (Word.Field fld in flds)

    Which will give you the field objects in the specified Range.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by janidbest Friday, July 19, 2013 4:51 AM
    Thursday, July 18, 2013 8:22 AM
    Moderator

All replies

  • What, exactly, do you want to do with the fields? You mention "replace" in the subject, but don't say what you want to replace them with. That makes it difficult to make any useful suggestions, other than:

    Word.Range rng = section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
    Word.Fields flds = rng.Fields;
    foreach (Word.Field fld in flds)

    Which will give you the field objects in the specified Range.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by janidbest Friday, July 19, 2013 4:51 AM
    Thursday, July 18, 2013 8:22 AM
    Moderator
  • Finally here it goes the actual code


                        // Loop through all sections
                        foreach (Microsoft.Office.Interop.Word.Section section in wordDocument.Sections)
                        {
                             wordDocument.TrackRevisions = false;//Disable Tracking for the Field replacement operation
                           //Get all Headers
                             Microsoft.Office.Interop.Word.HeadersFooters headers=section.Headers;
                            //Section headerfooter loop for all types enum WdHeaderFooterIndex. wdHeaderFooterEvenPages/wdHeaderFooterFirstPage/wdHeaderFooterPrimary;                          
                             foreach (Microsoft.Office.Interop.Word.HeaderFooter header in headers)
                             {
                                Fields fields= header.Range.Fields;
                                foreach (Field field in fields)
                                {
                                    if (field.Type == WdFieldType.wdFieldDate)
                                    {
                                        field.Select();
                                        field.Delete();
                                        wordApplication.Selection.TypeText("[DATE]");
                                    }
                                    else if (field.Type == WdFieldType.wdFieldFileName)
                                    {
                                        field.Select();
                                        field.Delete();
                                        wordApplication.Selection.TypeText("[FILE NAME]");

                                    }
                                }
                             }
                             //Get all Footers
                             Microsoft.Office.Interop.Word.HeadersFooters footers = section.Footers;
                             //Section headerfooter loop for all types enum WdHeaderFooterIndex. wdHeaderFooterEvenPages/wdHeaderFooterFirstPage/wdHeaderFooterPrimary; 
                             foreach (Microsoft.Office.Interop.Word.HeaderFooter footer in footers)
                             {
                                 Fields fields = footer.Range.Fields;
                                 foreach (Field field in fields)
                                 {
                                     if (field.Type == WdFieldType.wdFieldDate)
                                     {
                                         field.Select();
                                         field.Delete();
                                         wordApplication.Selection.TypeText("[DATE]");
                                     }
                                     else if (field.Type == WdFieldType.wdFieldFileName)
                                     {
                                         field.Select();
                                         field.Delete();
                                         wordApplication.Selection.TypeText("[FILE NAME]");

                                     }
                                 }
                             }
                        }

    • Edited by janidbest Friday, July 19, 2013 4:52 AM Updated result
    Friday, July 19, 2013 12:40 AM
  • May I make a suggestion for your code optimization? In Word, if at all possible, you should avoid working with Selection. There are cases when it's necessary, but usually you can get by without. And it's definitely better when dealing with the header/footer to use a Range object, instead. Something more like:

    Word.Range rngField = field.Result.Range;
    field.Delete();
    rngField.Text = "[Date]";


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, July 19, 2013 1:07 PM
    Moderator