如何更新页眉和页脚中的字段,而不仅仅是主文档?

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

在 C# 中,使用 wordDocument.Fields.Update() 可以很好地处理主体中的所有内容。

对于更新页眉和页脚中的字段的简洁优雅的方法有什么建议吗?

嗯,用过这个:http://blog.vanmeeuwen-online.nl/2009/09/update-all-fields-in-word-including.html,适配C#,只是漏掉了条件If aField.Type = Word.WdFieldType.wdFieldDocProperty.

工作得很好。

这个让我有点费劲。不得不深入研究 Iterop.Word 文档。 VB 中有大量此类示例,但我认为下面的代码是 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.");
        }
    }
}

我需要对 Word 文档中的每个字段执行更复杂的操作,因此我创建了一个更通用的迭代器扩展方法:

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 的示例代码现在可以更改为以下内容:

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.");
        }
    }
}