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
|