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

本文关键字:字段 文档 不仅仅是 何更新 更新 | 更新日期: 2023-09-27 18:07:24

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

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

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

我需要对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.");
        }
    }
}

这个花了我一点时间。我得在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.");
        }
    }
}

好吧,使用这个:http://blog.vanmeeuwen-online.nl/2009/09/update-all-fields-in-word-including.html,适应c#,只是省略了条件If aField.Type = Word.WdFieldType.wdFieldDocProperty

伟大工作。