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

Playing with a Full Deck

Ever need to populate some cells in your worksheet with a range of data, but in random order? Here's a handy macro to get the ...

Discover More

Word Styles and Templates

Styles are at the heart of Word's formatting power. Understanding how to use styles can greatly increase your ability to ...

Discover More

Stopping a Workbook from Persistently Auto-Loading

Excel has the capability to automatically open workbooks when you first start the program. You may not want to have one (or ...

Discover More

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!

More WordTips (ribbon)

Adding Circles around Letters or Numbers

Want to add some handy circles around text in your document? Believe it or not, Word provides three ways you can accomplish ...

Discover More

Replacing Text Selections

When editing a document, Word normally replaces whatever text you select with whatever you start to type. Here's how to turn ...

Discover More

Speeding Up Cursor Movement

If you use the arrow keys to move the insertion point through the document, you may have noticed that it can be slow going at ...

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 8Mpixels. 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 - 0?

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.