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

in C # using wordDocument.Fields.Update() works fine for everyone in the main part.

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

+4
source share
3 answers

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

Worked great.

+1
source

This one took me a bit. I had to dig a bit in 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."); } } } 
+4
source

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) { // 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; } } } } } 

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(); // 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."); } } } 
+2
source

All Articles