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

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

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

Numbering With Sequence Fields

You can use sequence fields for reliable numbering. They are quick, easy, and don't have some of the drawbacks of ...

Discover More

Specifying Chart Sizes

If you need a number of charts in your workbook to all be the same size, it can be a bother to manually change each of ...

Discover More

Copying Found Items to a New Document

Word allows you to use its searching capabilities to easily find multiple items in a document. What if you want to copy ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More WordTips (ribbon)

Pasting Clean Text

One of the most helpful tools in Word is the ability to paste straight text into a document. This is used so much on my ...

Discover More

An Automatic Two Spaces after a Period

Should you put two spaces after a sentence or just one? Ask different people and you will get different answers. To Word ...

Discover More

Entering a Page Break from the Keyboard

Need to force Word to move text to the top of the next page? It's easy when you use the keyboard shortcut for a page break.

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 three minus 3?

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.