Others

Use Word VBA to Replace Multiple Spaces in the Body and Footnotes

Khadija KhanKhadija Khan Nov 27, 2026 869 views

Question details

Replace consecutive spaces with a single space throughout a Word document, including footnotes and other document sections.

Replace Multiple Spaces in Word Body Text and Footnotes Using VBA
Product
Microsoft Word
Device & OS
not provided
Scenario
An editor wants to automate a recurring document cleanup step before publication.
Observed behavior
Repeated ordinary spaces are reduced to one space in the document body and accessible story ranges, including footnotes, endnotes, headers, and footers.
Before you start

Open the document in desktop Microsoft Word and make sure you can access the VBA editor. The macro below replaces ordinary spaces only; it does not change tabs or nonbreaking spaces.

Solution 1Recommended

Run a VBA macro across Word story ranges

Use this approach to clean repeated ordinary spaces in the body and other accessible document stories in one run.

Word stores the body, footnotes, endnotes, and headers and footers in separate story ranges. The macro follows each story range and its linked ranges, using wildcard Find and Replace without wrapping beyond the current range.

1
Open the VBA editor

In desktop Word, open the document, then choose Developer > Visual Basic. On Windows, you can also press Alt+F11.

2
Insert the macro

In the VBA editor, choose Insert > Module and paste this code: Sub ReplaceRepeatedSpaces() Dim story As Range Dim rng As Range For Each story In ActiveDocument.StoryRanges Set rng = story Do While Not rng Is Nothing With rng.Find .ClearFormatting .Replacement.ClearFormatting .Text = " {2,}" .Replacement.Text = " " .Forward = True .Wrap = wdFindStop .Format = False .MatchWildcards = True .Execute Replace:=wdReplaceAll End With Set rng = rng.NextStoryRange Loop Next story End Sub

3
Run and check the result

Place the cursor inside ReplaceRepeatedSpaces and choose Run > Run Sub/UserForm. Return to the document and check the body, footnotes, and relevant headers and footers.

Run a VBA macro across Word story ranges
Spaces and scope: The wildcard pattern matches two or more consecutive ordinary spaces and replaces each run with one ordinary space. It does not normalize tabs or nonbreaking spaces. Check any text in separate objects to confirm it was included.
Free Microsoft Office alternative

Edit Word Documents with WPS Writer

WPS Office is a free, lightweight Office alternative with a familiar interface and compatibility with Word formats for a smoother move between applications. Use WPS Writer to review and edit documents; it does not run the Word-specific VBA solution described above as a guaranteed substitute.

Review and edit document text in WPS WriterCompatible with Microsoft Word DOC and DOCX formatsFamiliar interface for everyday document editingFree and lightweight Office alternative
microsoft office alternative - wps office

Frequently Asked Questions

Will the macro also clean footnotes and endnotes?

Yes, it processes accessible footnote and endnote story ranges returned by ActiveDocument.StoryRanges, along with their linked ranges.

Does the macro replace tabs or nonbreaking spaces?

No. The Find pattern contains an ordinary space, so it only reduces runs of ordinary spaces. Tabs and nonbreaking spaces require separate handling.

Why use wdFindStop instead of wdFindContinue?

wdFindStop keeps each Find and Replace operation within the current range. The macro then advances to the next linked story range with NextStoryRange.

Can I run this macro in Word for the web?

No. Open the document in desktop Microsoft Word and use Developer > Visual Basic to insert and run the VBA macro.