Is it possible to do a find and replace all and add fields in the replace
all? I'd like to search a doc for the following text ?Input? and replace this
text with a form field. I know how to do a find a replace all and I know how
to add a form field, but not together.
Any help would be greatly appreciated.
Thanks,
Dave
Hi Dave,
You can use Find to locate the markers, but you can't replace with a
field. Instead, you use ActiveDocuments.FormFields.Add and give it the
range containing the found marker. The form field will replace the
text of the marker by the usual "typing replaces selection" action.
Try this code:
Sub MakeFormFields()
Dim oRg As Range
Set oRg = ActiveDocument.Range
With oRg.Find
.Format = False
.Forward = True
.MatchWildcards = False
.Wrap = wdFindStop
.Text = "?Input?"
Do While .Execute
ActiveDocument.FormFields.Add _
Range:=oRg, Type:=wdFieldFormTextInput
Loop
End With
End Sub
This creates the fields with their default names (Text1, Text2, etc.).
If you want the macro to change the names as they're added, you'd
rewrite the loop to something like this:
Dim nIndex As Integer
Dim oFld As FormField
Do While .Execute
nIndex = nIndex + 1
Set oFld = ActiveDocument.FormFields.Add( _
Range:=oRg, Type:=wdFieldFormTextInput)
oFld.Name = "MyFieldName" & nIndex
Loop