Update Path of Field in Headers and Footers
I link fields from Excel to Word. What I am trying to do is be able to change the path of the fields in word and then update them through VBA. Once I get the code working, I will work on a user form, where the original and replacement path strings can be entered; however, I am getting ahead of myself. I have the following code, which works for the fields in the main body of word, but has no effect on the fields in the headers and footers. Any help on why it is not working would be greatly appreciated.
Sub ChangePathInLinkField()
Dim doc As Word.Document
Dim fld As Word.Field
Dim rng As Word.Range
Dim strSearchPath As String
Dim strReplacePath As String
Dim strNewFieldCode As String
Set doc = ActiveDocument
strSearchPath = "OldFilePath"
strReplacePath = "NewFliePath"
'' changes paths of fields in document
''
For Each fld In doc.Fields
If fld.Type = wdFieldLink Then
strNewFieldCode = Replace(fld.Code.Text, strSearchPath, strReplacePath)
fld.Code.Text = strNewFieldCode
End If
Next
doc.Fields.Update
'' changes paths of fields in header/footer
''
For Each rng In doc.StoryRanges
For Each fld In rng.Fields
If fld.Type = wdFieldLink Then
strNewFieldCode = Replace(fld.Code.Text, strSearchPath, strReplacePath)
fld.Update
End If
Next fld
Next rng
End Sub
|