Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, and 2013. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Strip Trailing Spaces.

Strip Trailing Spaces

by Allen Wyatt
(last updated February 28, 2015)

5

Maybe I'm one of those compulsive-obsessive types, but I always go through my documents to make sure there are no trailing spaces at the end of a paragraph. This makes the document neater and smaller. If you format ASCII files or documents you receive from other people, you will need to search for trailing spaces and remove them as you format the file for Word. The following macro, StripSpaces, takes out all spaces before paragraph marks and manual line breaks throughout your document.

Sub StripSpaces()
    Selection.HomeKey Unit:=wdStory
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = " ^p"
        .Replacement.Text = "^p"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchWholeWord = False
        .MatchWildcards = False
    End With
    Selection.Find.Execute
    While Selection.Find.Found
        Selection.HomeKey Unit:=wdStory
        Selection.Find.Execute Replace:=wdReplaceAll
        Selection.Find.Execute
    Wend

    Selection.Find.Text = " ^l"
    Selection.Find.Replacement.Text = "^l"
    Selection.Find.Execute
    While Selection.Find.Found
        Selection.HomeKey Unit:=wdStory
        Selection.Find.Execute Replace:=wdReplaceAll
        Selection.Find.Execute
    Wend
End Sub

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (13376) applies to Microsoft Word 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Word here: Strip Trailing Spaces.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Tracking Down Invalid References

When you discover that there is an invalid reference in a workbook, you can have a bear of a time tracking down the problem. ...

Discover More

Limiting a Spelling Check

When you perform a spelling check, Word typically checks everything in your document. If you want to limit what is checked, ...

Discover More

Using COUNTIF with Colors

Excel allows you to easily format cells with different fonts, borders, and colors. If you want to count the number of cells ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More WordTips (ribbon)

Working with Master and Subdocuments

Word has long had the capability of establishing relationships between documents by designating some as master documents and ...

Discover More

Selecting a Line of Text

Many word processing programs include commands that allow you to select a line of text. Word doesn't, but you can use the ...

Discover More

Deleting a Range of Pages

Need to delete a range of pages out of the middle of your document? It's easy to do using editing techniques you already know ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 7 - 1?

2017-07-26 12:34:58

Andres Reyes

I am using Word 2013 (15.0.4701.1001) MSO (15.0.4701.1000) 32-bit. When I use the macro, there is a lot of fast blinking of the first page, then a pause for a second or so and then blinking again. This sequence repeats a many times. Then the blinking stops, but the 'busy' circle shaped mouse pointer stays on. I let it wait for over an hour and then clicked on the window. The window became 'dim'. Task manager reported Word as not responding. When I click on the 'x' in the window I get the not responding popup to wait of restart.

The document is 86 pages long and contains many tables.

I copied and pasted the macro, so I know that it is what you published.

Any ideas?


2015-03-04 16:32:22

Steve

Use '^w^p' to clear all "white space" prior to a paragraph. This will get 1 or 2 or more spaces, as well as any tabs that may be there. Use '^w^l" to clear out the white space prior to a manual line break. A macro is unnecessary and I find that trying to figure out where to store the is more trouble than it is worth in many cases.


2015-03-03 10:18:57

Damear

Lisa, you are right, except the idea is to strip trailing spaces, as this is what the article is about. I just thought that a paragraph can end with more than one space, so I added the suggestion to repeat the command several times.


2015-03-02 08:33:17

Lisa Herider

Damear, I believe your replace dialogue will only get the multiple spaces before a paragraph return. Using the same approach just search for a double spaceband (space space) and replace it with a single. Again, you will need to do this multiple times until 'Replacements made:' shows 0. Using this method finds all instances of multiple spaces regardless of what's before or after them.


2015-03-01 08:20:43

Damear

Or just press Ctrl-H for Find and Replace dialog, then enter ' ^p' (space, caret, p) in the Find What field and '^p' (same as above, but without space) in the Replace field. Then press Replace All button. Sometimes this step has to be repeated several times if there are multiple trailing spaces in some paragraphs. Watch the dialog window that pops up stating 'Replacements made:' until it shows 0.


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.