Replace Field in Header&Footer in Word Using Interop

Finally after going through the poor documentation about introp.word, got the solution

// 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]");

            }
        }
    }
}

Jay,

Maybe a bit late but anyway ...

Can't comment so I answer.

Few things I though may be useful to you (or others) one day in terms of current accepted answer.

  1. To answer your question from the last Update, you can use something like this to turn on field codes and then use Find to search for fields stuff:

    wordDocument.ActiveWindow.View.ShowFieldCodes = true;
    

    So, you would turn that ON before searching (unless on already) and restore it back when you are done.

  2. The solution you provided to yourself will work for most scenarios and I used something like that for some time. However, I bumped into a document with 2000 sections. And looping through those sections will in turn loop through the same header(s) over and over again. In my case processing the document timed out (given the acceptable processing time)

  3. the solution with StoryRanges might be a better approach (combined with switching field codes)
    Some examples of using it (generic search & replace):
    http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm
    https://wls.wwco.com/blog/2010/07/03/find-and-replace-in-word-using-c-net/

  4. One thing to remember: don't forget to search for things in Shapes of Range

  5. I guess you figured out how to replace the field. In any case, I am actually converting simple text to a field.

    Once Find.Execute has hit something, the range will be selected and I do

    theDoc.Fields.Add(range, WdFieldType.wdFieldDocVariable, "myDocVar");
    

TL;DR: If your documents are predictable in format and only have a small number of sections and text is not within shapes then don't worry about all this.