PDA

View Full Version : Find and Replace text with a field



Adamski
08-17-2011, 05:21 AM
Hi,

Using Word 2010, I would like to replace all instances of a string with a field. The problem is the field it replaces it with contains the string. This then gets found again. Is there a way to exclude fis from the find? Otherwise I guess I just test each find range to check if it is in a field. But then I need to somehow end the find when everything remaining is just the fields.

Simplified example: replace "Test" with "{ QUOTE Test}"

I actual want to replace with a much more complex nested field but don't worry about that.

Thanks

macropod
08-24-2011, 01:05 AM
Try something along the lines of:
Sub FRFields()
Dim oRng As Range, oFld As Field
With ActiveDocument
' Loop through all range objects and accept tracked changes on fields
For Each oRng In .StoryRanges
Do
For Each oFld In oRng.Fields
With oFld
.Code.Text = Replace(.Code.Text, "String to Find", "Replacement String")
Next
Set oRng = oRng.NextStoryRange
Loop Until oRng Is Nothing
Next
End With
End Sub