Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use Word's Built-In Replace Dialog in FormFields Only

Status
Not open for further replies.

ACH381

Technical User
Apr 3, 2002
49
US
I have a large form with hundreds of formfields. I need to use a find and replace function, preferably Word's wdDialogEditReplace dialog to find and replace words or phrases in formfields only. I can invoke the dialog box from script, but to do so I have to unlock the entire document; thus exposing words and phrases in captions, instructions, etc., that cannot be changed.

Does anyone have a find and replace module built that I can use? OR Does anyone know of a way to use Words built-in dialog box to manipulate text in FormFields only?
 
This works. In my test, the userform has two textboxes. I did not bother to rename them, so they are still Textbox1 and Textbox2. They do have Labels above them with "Find", and "Replace". One commandbutton.

In standard module:
Code:
Option Explicit

Sub FIndReplaceFormfields()
UserForm1.Show
End Sub

In Userform1 module:
Code:
Private Sub CommandButton1_Click()
Dim DocFF As FormFields
Dim oFF As FormField
Dim strFind As String
Dim strReplace As String

strFind = TextBox1.Text
strReplace = TextBox2.Text
Set DocFF = ActiveDocument.FormFields
   For Each oFF In DocFF
      If InStr(1, oFF.Result, strFind) > 0 Then
         oFF.Result = Replace(oFF.Result, strFind, strReplace)
      End If
   Next
Set DocFF = Nothing
Unload Me
End Sub
I put the procedure to call the userform as a text icon ("Find Replace") on a toolbar. You could have it as a keyboard shortcut, or whatever other way you want to call it.

Click "Find Replace" on the toolbar and the userform displays.

Enter the find text into the Find textbox.
Enter the replace text into the Replace textbox.
Click the commandbutton.

For each formfield, it checks to see if the .Result contains the Find string. If it does, then it replaces the Find string with the Replace string.

The document does NOT need to be unprotected.

faq219-2884

Gerry
My paintings and sculpture
 
Thanks I'll give it a try and let you know how it works. I'd like to be able to walk through the report using something like Find Next to let the user find each instance of the word or phrase and replace only what he wants to and not replace everything automatically.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top