Word

Replace text in all formfields

Ease of Use

Easy

Version tested with

2000, 2002 

Submitted by:

MOS MASTER

Description:

This will replace the word you're looking for with the replacement word you specify in all formfields of type "wdFieldFormTextInput" that have TextInput type to: "wdRegularText" in the active document. 

Discussion:

Sometimes you have filled in all your formfields and you want those Words checked for a certain word and if found replace it with another word. Note: This will not work in Office '97 cause that version has no "Replace" Function. 

Code:

instructions for use

			

Option Explicit Option Compare Text Sub TestReplace() ' \\Call the sub and look for VBA and replace with VBAX! Call ReplaceInFormfieldResult("VBA", "VBAX") End Sub Private Sub ReplaceInFormfieldResult(sFind As String, sReplace As String) Dim oFld As Word.FormField With Application.ActiveDocument ' \\ TextInput Type requires to unprotect the document If .ProtectionType <> wdNoProtection Then .Unprotect ' \\ Loop all formfields in active document For Each oFld In .FormFields() ' \\ Only replace in Formfield textboxes that have textinput only If oFld.Type = wdFieldFormTextInput And oFld.TextInput.Type = wdRegularText Then ' \\ Look for sFind and replace with sReplace ' \\ Start at beginning and replace all oFld.Result = _ Replace(oFld.Result, sFind, sReplace, 1, -1, vbTextCompare) End If Next ' \\ Reprotect the document .Protect wdAllowOnlyFormFields, True End With End Sub

How to use:

  1. Open your Word document.
  2. Press Alt + F11 to open VBE.
  3. Insert-Module. (Insert -> module)
  4. Paste the code there in the window at right. (F7)
  5. Close VBE (Alt + Q or press the X in the top right hand corner).
  6. Save the file.
 

Test the code:

  1. Use Tools | Macro | Macro's and doubleclick: "TestReplace"
  2. Or use the button in the test document.
 

Sample File:

Replace in Formfield text.zip 10.6KB 

Approved by mdmackillop


This entry has been viewed 171 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express