Gabe
Gabe

Reputation: 632

How to update fields in headers and footers, not just main document?

in C#, using wordDocument.Fields.Update() works great on everything in the main body.

Any suggestion for concise and elegant way to update the fields in the headers and footers as well?

Upvotes: 9

Views: 3760

Answers (3)

Dan Stevens
Dan Stevens

Reputation: 6840

I needed to perform more complex operations with each field in a Word document so I created a more general iterator extention method:

public static class WordDocument
{
    public static IEnumerable<Field> GetAllFields(this Document _document)
    {
        // Main text story fields (doesn't include fields in headers and footers)
        foreach (Field field in _document.Fields) {
            yield return field;
        }

        foreach (Section section in _document.Sections) {
            // Header fields
            foreach (HeaderFooter header in section.Headers) {
                foreach (Field field in header.Range.Fields) {
                    yield return field;
                }
            }

            // Footer fields
            foreach (HeaderFooter footer in section.Footers) {
                foreach (Field field in footer.Range.Fields) {
                    yield return field;
                }
            }
        }
    }
}

@Sherd's example code can now be changed to the following:

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Office.Interop.Word;

class Program
{
    static void Main(string[] args)
    {
        List<string> path = new List<string>(args);
        string filePathstr = string.Join(" ", path.ToArray());
        string folderPathstr = Path.GetDirectoryName(filePathstr);

        try
        {
            Application ap = new Application();
            Document document = ap.Documents.Open(filePathstr);

            foreach (Field field in document.GetAllFields())
            {
                field.Update();
                
                // Other stuff with field can be done here as well
            }    

            document.Save();
            document.Close();

        }
        catch (NullReferenceException)
        {
            System.Windows.Forms.MessageBox.Show("A valid file was not selected.");
        }
    }
}

Upvotes: 8

Sherd
Sherd

Reputation: 468

This one took me a bit. Had to dig a bit into the Iterop.Word documentation. Tons of examples of this in VB, but I think my code below is a good example of this in C#.

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Office.Interop.Word;

class Program
{
    static void Main(string[] args)
    {
        List<string> path = new List<string>(args);

        string filePathstr = string.Join(" ", path.ToArray());
        //System.Windows.Forms.MessageBox.Show("filepathstr: " + filePathstr);

        string folderPathstr = Path.GetDirectoryName(filePathstr);
        //System.Windows.Forms.MessageBox.Show("folderPathstr: " + folderPathstr);

        try
        {
            Application ap = new Application();
            Document document = ap.Documents.Open(filePathstr);
            document.Fields.Update();

            foreach (Section section in document.Sections)
            {
                document.Fields.Update();  // update each section

                HeadersFooters headers = section.Headers;  //Get all headers
                foreach (HeaderFooter header in headers)
                {
                    Fields fields = header.Range.Fields;
                    foreach (Field field in fields)
                    {
                        field.Update();  // update all fields in headers
                    }
                }

                HeadersFooters footers = section.Footers;  //Get all footers
                foreach (HeaderFooter footer in footers)
                {
                    Fields fields = footer.Range.Fields;
                    foreach (Field field in fields)
                    {
                        field.Update();  //update all fields in footers
                    }
                }
            }    

            document.Save();
            document.Close();

        }
        catch (NullReferenceException)
        {
            System.Windows.Forms.MessageBox.Show("A valid file was not selected.");
        }
    }
}

Upvotes: 7

Gabe
Gabe

Reputation: 632

Well, used this: http://blog.vanmeeuwen-online.nl/2009/09/update-all-fields-in-word-including.html, adapting to C#, just left out the condition If aField.Type = Word.WdFieldType.wdFieldDocProperty.

Worked great.

Upvotes: 1

Related Questions