I needed to perform more complex operations with each field in a Word document, so I created a more general iterator extension method:
public static class WordDocument { public static IEnumerable<Field> GetAllFields(this Document _document) {
Now the source sample code can 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();
source share