Add Fields Using Find And Replace

D

Dave

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
 
J

Jay Freedman

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
 
D

Dave

Thanks for the response, I'm trying both suggestions and I'm sure one of
these will do the trick.

Thanks again!!

Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top