Microsoft 365 and Office | Word | For home | Windows
A family of Microsoft word processing software products for creating web, email, and print documents.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hell from Steve
The below script is not functioning, What is required please
Sub Rename_Trainers()
Dim FindWords As Variant, ReplaceWords As Variant
Dim i As Integer
FindWords = Array("MCKAY HARRINGTO", "WALKER BERGERSO", "O'SULLIVAN SCOT", "RICHARDSON NORV, _
"JAMES/WELLWOOD")
ReplaceWords = Array("MCKAY HARRINGTON", "WALKER BERGERSON", "O'SULLIVAN SCOTT", "RICHARDSON NORVALL, _
"JAMES WELLWOOD")
For i = LBound(FindWords) To UBound(FindWords)
With ActiveDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Font.Size = 12
.Text = FindWords(i)
.Replacement.Text = ReplaceWords(i)
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll, Format:=True
End With
Next i
End Sub
A family of Microsoft word processing software products for creating web, email, and print documents.
Two syntax issues prevent the macro from running:
Array lines are broken incorrectly with _ inside the string list._.Use this corrected version:
Sub Rename_Trainers()
Dim FindWords As Variant, ReplaceWords As Variant
Dim i As Long
FindWords = Array( _
"MCKAY HARRINGTO", _
"WALKER BERGERSO", _
"O'SULLIVAN SCOT", _
"RICHARDSON NORV", _
"JAMES/WELLWOOD")
ReplaceWords = Array( _
"MCKAY HARRINGTON", _
"WALKER BERGERSON", _
"O'SULLIVAN SCOTT", _
"RICHARDSON NORVALL", _
"JAMES WELLWOOD")
For i = LBound(FindWords) To UBound(FindWords)
With ActiveDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Font.Size = 12
.Text = FindWords(i)
.Replacement.Text = ReplaceWords(i)
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll, Format:=True
End With
Next i
End Sub
Key points:
Array call is on one logical line; line continuation _ is placed outside the quotes and after a comma.LBound/UBound loop matches the number of elements in both arrays.This will search the whole document (ActiveDocument.Content.Find) and replace each misspelled name with the corrected one, using the Find/Replacement pattern described in the Word VBA documentation.
References: