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

Inserting a Non-Breaking Hyphen

Non-breaking hyphens can come in helpful for some types of writing. They force the words (or characters) on both sides of the ...

Discover More

Getting Rid of Fragment Warnings

Word provides a wide variety of tools that ostensibly help make you a better writer. One of those tools is the grammar ...

Discover More

Alphabetizing By Last Name

Sorting lists of names by last name is easy if they are listed in a LAST, FIRST order. But what if the names are in FIRST ...

Discover More

Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!

More WordTips (ribbon)

Automatic Non-breaking Spaces in Dates

It drives some people crazy to have a date break across two lines. If you find yourself in this mindset, then you'll ...

Discover More

Adding Half Spaces to Punctuation

Want a little more space just before some of your punctuation characters? You can add that spacing in a variety of ways, as ...

Discover More

Selecting the Entire Document with the Mouse

Want a quick way to select your entire document without taking your hand off of the mouse? Try clicking away using the ...

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 6 - 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.