Word Replace Names

SteveD 585 Reputation points
2026-07-21T21:30:44.88+00:00

Hello from Steve

The below is giving me an

compile error:

syntax

What is required please for the below to function.

Sub Trainers_Names()

Dim rngStory As range

For Each rngStory In ActiveDocument.StoryRanges

With rngStory.Find

  .Text = "O'SULLIVAN SCOT","RAMSAY/RITCHIE"

  .Replacement.Text = "O'SULLIVAN SCOTT","RAMSAY RITCHIE"

  .Wrap = wdFindContinue

  .Execute Replace:=wdReplaceAll

End With

Next rngStory

lbl_Exit:

Exit Sub

End Sub

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

Answer accepted by question author

Marcin Policht 96,925 Reputation points MVP Volunteer Moderator
2026-07-21T21:56:54.0833333+00:00

The compile error occurs because the .Text property and the .Replacement.Text property can each contain only one string. AFAIK, VBA does not allow multiple values separated by commas in a property assignment.

Instead of:

.Text = "O'SULLIVAN SCOT","RAMSAY/RITCHIE"

try performing a separate search and replace for each pair.

For example:

Sub Trainers_Names()

    Dim rngStory As Range

    For Each rngStory In ActiveDocument.StoryRanges

        With rngStory.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Wrap = wdFindContinue

            .Text = "O'SULLIVAN SCOT"
            .Replacement.Text = "O'SULLIVAN SCOTT"
            .Execute Replace:=wdReplaceAll

            .Text = "RAMSAY/RITCHIE"
            .Replacement.Text = "RAMSAY RITCHIE"
            .Execute Replace:=wdReplaceAll
        End With

    Next rngStory

End Sub

If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

hth

Marcin

Was this answer helpful?

2 people found this answer helpful.

0 additional answers

Sort by: Most 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.