Run Word VBA Find and Replace Across Story Ranges
Question details
The user needs a Word VBA macro to apply multiple find-and-replace pairs across document stories, including endnotes.

- Product
- Microsoft Word VBA
- Device & OS
- not provided
- Scenario
- A macro uses arrays of search and replacement terms and also needs to clean up repeated spaces and spaces next to paragraph marks.
- Observed behavior
- The desired result is consistent replacement in the main document, endnotes, and other accessible story ranges.
Open the document in a desktop version of Word that supports VBA, and make sure macros are permitted by your organization's security settings. Test the cleanup patterns on a copy first, especially if spaces at the start or end of paragraphs are intentional.
Loop through Word story ranges and apply each replacement pair
Use this macro to process each available story and its linked ranges, including endnotes, with wildcard cleanup patterns.
The example uses equally sized Variant arrays created with Array, so every search term has a corresponding replacement. It follows NextStoryRange to reach linked ranges such as headers and footers.
All patterns in this example use Word wildcards. If you add literal search terms containing wildcard characters, escape those characters or run the literal replacements separately with MatchWildcards set to False.
In Word, open the document, then select Developer > Visual Basic. In the VBA editor, choose Insert > Module.
Paste the following code into the module: Sub ReplaceAcrossStories() Dim findTerms As Variant, replaceTerms As Variant Dim firstStory As Range, currentStory As Range, workRange As Range Dim i As Long findTerms = Array("[ ]{2,}", "[ ]{1,}^13", "^13[ ]{1,}") replaceTerms = Array(" ", "^p", "^p") For Each firstStory In ActiveDocument.StoryRanges Set currentStory = firstStory Do While Not currentStory Is Nothing For i = LBound(findTerms) To UBound(findTerms) Set workRange = currentStory.Duplicate With workRange.Find .ClearFormatting .Replacement.ClearFormatting .Text = findTerms(i) .Replacement.Text = replaceTerms(i) .Forward = True .Wrap = wdFindStop .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = True .Execute Replace:=wdReplaceAll End With Next i Set currentStory = currentStory.NextStoryRange Loop Next firstStory End Sub
Place the cursor inside ReplaceAcrossStories and select Run > Run Sub/UserForm. Return to the document and check the main text, endnotes, and other areas affected by the cleanup.

Edit Word documents with WPS Writer
WPS Office is a free, lightweight alternative with a familiar interface for editing Word documents and moving between office suites. Word VBA macro compatibility can vary, so use Microsoft Word to run and verify the macro in this article.

Frequently Asked Questions
How do I target only the endnotes story?
If ActiveDocument.Endnotes.Count is greater than zero, use ActiveDocument.StoryRanges(wdEndnotesStory) to obtain the endnotes range, then run Find on a duplicate of that range.
Why should I use NextStoryRange?
Each item in ActiveDocument.StoryRanges is the first range of a story type. Following its NextStoryRange links lets the macro process additional linked ranges, such as headers or footers in other sections.
How can I add more find-and-replace pairs?
Add a search pattern to findTerms and its corresponding replacement at the same position in replaceTerms. Keep both arrays the same length; the example loops from LBound(findTerms) through UBound(findTerms).
What do the cleanup patterns change?
The pattern [ ]{2,} reduces runs of spaces to one space. The other two patterns remove spaces immediately before or after a paragraph mark, using ^13 in the wildcard search and ^p in the replacement.




