Today, I rewrote a new piece of code I found, and that seems to work fine. I created an extension method for the Word.Document object that does the trick.
_
Sub UpdateAllFiels(ByVal doc As Word.Document)
Dim section As Word.Section
Dim myRange As Word.Range
Dim myHF As Word.HeaderFooter
doc.Fields.Update()
For Each section In doc.Sections
For Each myHF In section.Headers
myRange = myHF.Range
For Each aField In myRange.Fields
If aField.Type = Word.WdFieldType.wdFieldDocProperty Then
aField.Update()
End If
Next aField
Next myHF
For Each myHF In section.Footers
myRange = myHF.Range
For Each aField In myRange.Fields
If aField.Type = Word.WdFieldType.wdFieldDocProperty Then
aField.Update()
End If
Next aField
Next myHF
Next section
End Sub
So, now I am able to update all fields easily in my document. In my Word Add-on, I created a own ribbon tab, I placed a button on it, which does the following:
Globals.ThisAddIn.Application.ActiveDocument.UpdateAllFiels()
Note: Extension methods is supported on .NET framework 3.5 or higher
Edit: A little fix: 'doc.Fields.Update()' added to the code. This is the regular Word Update All Fields function (so without doing Headers and Footers).
No comments:
Post a Comment