Share via

Word script is not functioning.

SteveD 145 Reputation points
2026-04-02T23:06:03.0333333+00:00

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

Microsoft 365 and Office | Word | For home | Windows
0 comments No comments

1 answer

Sort by: Most helpful
  1. Q&A Assist
    2026-04-02T23:06:15.5833333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    Two syntax issues prevent the macro from running:

    1. The Array lines are broken incorrectly with _ inside the string list.
    2. The quotes are not properly closed on the lines with _.

    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:

    • Each Array call is on one logical line; line continuation _ is placed outside the quotes and after a comma.
    • Every string is fully enclosed in quotes.
    • 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:

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.