Document Editing Problems

Run Word VBA Find and Replace Across Story Ranges

Rana GarciaRana Garcia Nov 27, 2026 868 views

Question details

The user needs a Word VBA macro to apply multiple find-and-replace pairs across document stories, including endnotes.

Run Word VBA Find and Replace Across Story Ranges
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA editor

In Word, open the document, then select Developer > Visual Basic. In the VBA editor, choose Insert > Module.

2
Add the cleanup macro

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

3
Run and check the macro

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.

Loop through Word story ranges and apply each replacement pair
Check array pairs and story coverage: Keep the find and replacement arrays the same length and in matching order. StoryRanges and NextStoryRange cover available Word stories, but some text boxes or other objects may require separate handling; inspect those areas after running the macro.
Free Microsoft Office alternative

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.

Edit document text and use Find and Replace in WPS WriterOpen and edit Microsoft Word DOC and DOCX filesWork in a familiar interface when moving from Microsoft OfficeUse a free, lightweight office suite for everyday document editing
microsoft office alternative - wps office

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.