Trailing spaces in word

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

Written by Allen Wyatt (last updated June 26, 2018)
This tip applies to Word 97, 2000, 2002, and 2003


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 line. 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, will take 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

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 (969) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Strip Trailing Spaces.

Author Bio

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. Learn more about Allen…

MORE FROM ALLEN

Saving Document Versions

Documents often go through several versions during development. For this reason, Word provides a feature that allows you …

Discover More

Creating Worksheets with a Macro

Using a macro to add worksheets to your workbook is easy. This tip provides two different methods you can use.

Discover More

Increasing Font Size In Worksheet Tabs

While Excel does not have an intrinsic way to change the font in on a worksheet tab, Windows does.

Discover More

More WordTips (menu)

Comparing Documents Top and Bottom

Word has a feature that allows you to compare two documents side-by-side. What if you actually want to compare the …

Discover More

Entering a Degree Sign

One of the more common symbols that people need to use in their writing is the degree symbol, typically used after a …

Discover More

Picking Up Where You Left Off

Need a quick way to get back to a where you previously edited? Here’s a shortcut that will serve you well.

Discover More

I have opened a PDF file in Word in order to edit it (the original Word document is lost). During the conversion, each line of text and each table cell was given an extra space. Of course, they don’t really hurt anyone except me and my slight OCD. I don’t like trailing whitespace.

Screenshot

It’s trivial to remove a trailing space at the end of a line (search for " ^p" and replace with "^p"), but there doesn’t seem to be a special character for «end of table cell marker» in the find/replace dialog, and the Word «regex» engine doesn’t appear to support anchors either.

A Google search brought me to this tip on how to add something to the end of a table cell (I planned to add something like "§§§" to each cell and then do a simple search/replace for " §§§"), but in Word 2016, I can’t find the Style «Table cell», at least not in the German version of Word.

Any other idea I could try? Also, any idea how to remove one or more spaces at the end of a line/table cell? Quantifiers don’t seem to work in a non-regex find/replace.

asked Jan 19, 2016 at 8:39

Tim Pietzcker's user avatar

Tim PietzckerTim Pietzcker

2,64010 gold badges42 silver badges49 bronze badges

3

OK, I’ve been digging around the Word VBA object model and came up with this (Microsoft VBScript Regular Expressions 5.5 enabled):

Sub TrimCellSpaces()
    Dim myRE As New RegExp
    Dim itable As Table
    Dim C As Cell
    myRE.Pattern = "s+(?!.*w)"
    For Each itable In ThisDocument.Tables
        For Each C In itable.Range.Cells
            With myRE
                C.Range.Text = .Replace(C.Range.Text, "")
            End With
        Next
    Next
End Sub

Interestingly enough, my inital attempt (using Trim(C.Range.Text)) didn’t remove the spaces, on the contrary, it added a paragraph marker at the end of each cell. Puzzled by this, I tried a regex s+$ with the same results. Inspecting the local object tree, I found that a cell that contained the text Note  actually contained Note x0dx0dx07 (I have since found out why this is the case). That’s why I had to use that weird regex to find the last spaces in a cell…

Community's user avatar

answered Jan 19, 2016 at 14:02

Tim Pietzcker's user avatar

Tim PietzckerTim Pietzcker

2,64010 gold badges42 silver badges49 bronze badges

I use a modification of Tim Pietzcker’s Sub, where I use combination of Left() and Trim() instead of regExp (and also use the ActiveDocument instead of ThisDocument):

Sub TrimCellSpaces()
    Dim itable As Table
    Dim C As Cell
    For Each itable In ActiveDocument.Tables
        For Each C In itable.Range.Cells
            C.Range.Text = Trim(Left(C.Range.Text, Len(C.Range.Text) - 2))
        Next
    Next
End Sub

answered Apr 24, 2018 at 11:16

Roman Tarasiuk's user avatar

The guide you found appears to contain the answer: assign a style to the affected cells.

If needed, create a new style just for this purpose based on the style that the cells already have. You can then go back to the previous style, or leave the new style in place.

answered Oct 7, 2019 at 21:16

Speedbird186's user avatar

  • Remove From My Forums
  • Question

  • Is there a way to control the behavior of double-click for word selection so that the spaces (if exist) after the selected word will not be part of the selection? For example, I have text «word1 word2» and I double-click on word1. I don’t want
    the space after «word1» to be selected. If the text is «word         «, selecting «word» would include all trailing spaces.

    This may not be a problem with Outlook, but with Windows. But I’m still curious about a possible solution. Thanks.

Answers

    • Proposed as answer by

      Monday, October 10, 2016 4:52 AM

    • Marked as answer by
      Winnie LiangMicrosoft contingent staff
      Tuesday, October 11, 2016 10:21 AM
    • Edited by
      chcw
      Sunday, April 9, 2017 1:00 PM
  • As explained by chcw, this is by design.

    If you select a word from the middle a sentence, copy it and then paste it into another sentence you want the trailing space on the clipboard because otherwise you would have to take additional steps to insert separating spaces after the paste. If you cut
    the selected text, then you most likely wouldn’t want extra spaces left behind. If you paste the selected text at the start of another sentence then you again want the include space separator.

    Word automatically does a number of «usually» time saving and keystroke saving shortcuts to help you, and this is one of them. It’s not perfect, nor can it be… but hopefully shortcuts like these, on average, save you time and make your document
    editing easier.


    Kind Regards, Rich … http://greatcirclelearning.com

    • Proposed as answer by
      Winnie LiangMicrosoft contingent staff
      Monday, October 10, 2016 4:52 AM
    • Marked as answer by
      Winnie LiangMicrosoft contingent staff
      Tuesday, October 11, 2016 10:21 AM

Понравилась статья? Поделить с друзьями:
  • Top class a word
  • Translate bit to word
  • Traditions and language lesson plan 9 grade excel
  • Top and bottom margin in word
  • Translate any word to japanese