Aboot this blog

This blog is a cross between a scrapbook and a diary. I hope you find something here of interest. If you’d like to keep up with things as I see them then you can subscribe to my news feed by clicking the icon below:



Please note that any reviews/recommendations are based solely on my own experience and does not constitute a guarantee that you will have the same experience. Please do your own research before parting with any money - the risk is all yours!

The Briticiser

Looking for my world famous US to GB English spelling converter?





  Utilities     Browsers         Software     Reviews     Bits and Bobs     VBA     Flatpress     Family     News     Hardware     Bookmarks     Acrobat     Overcoming Disabilities     Equipment     Word     Windows     Odds and Sods     Tutorials     Plausible Lies     Excel     PHP     Education  

Find me on Facebook

facebook-icon-28x28.pngVisit my Briticiser Facebook Page to keep up with things here - lots of reviews of stuff I’ve really used and also all kinds of helpful computer tips with a heavy bias towards SAVING YOU TIME.

Removing unwanted white spaces in MS Word

Permalink: http://paul.us2uk.eu/?x=entry:entry130605-170650

[updated May 2015 - VBA code updated to check for a hyperlink in a table cell, and just remove the white space from the displayed text while leaving the hyperlink intact]

It may happen that you need to remove white spaces from the ends of lines in Word documents - not just in the main text, but in table cells too.

Removing white spaces from the ends of lines in normal paragraphs is relatively easy - just do a find and replace of ” ^p” (that’s a space followed by the “hat” character which you can usually get by typing Shift+6) followed by a p, with “” (nothing). “^p” stands for the paragraph mark, or pilcrow (ΒΆ) which you can see if you show all of the non-printing characters in your document.

This will instantly rid your document of all those unwanted spaces. But what about the same thing in table cells? For this we need to box a bit cleverer.

There are two ways to do this. One is by using a macro and the other is a neat and effective “trick” which you can achieve with just three mouse clicks:

Click 1 - select the table column(s) containing the rogue spaces

Click 2 - click one of the text alignment buttons on the toolbar to change the current alignment (e.g. if the text is centred, then left align it, and do the reverse if it’s already left-aligned).

Click 3 - click the relevant alignment button to change the alignment back to the way it was.

All of those spaces at the end of the lines in the cells will magically disappear.

The other way (the macro, for those gifted enough to know where to put this code) is as follows [updated May 2015]:

Sub eocspace()
' eocspace Macro
' Macro to remove white space before the end-of-cell marker in table cells
' If a hyperlink is found in the cell then it just removes the white space in the displayed text leaving the hyperlink intact
Dim oRng As Range
Dim oTable As Table
Dim acell As Cell
For Each oTable In ActiveDocument.Tables
    With oTable
        For Each acell In oTable.Range.Cells
            Set oRng = acell.Range
            If oRng.Hyperlinks.Count <> 1 Then 'checks first that this is NOT a hyperlink
                oRng.End = oRng.End - 1
                oRng.Text = RTrim(oRng.Text)
            Else                               'if it IS a hyperlink then just trim the displayed text
                oRng.Hyperlinks(1).TextToDisplay = RTrim(oRng.Hyperlinks(1).TextToDisplay)
            End If
        Next acell
    End With
Next oTable
End Sub

Ching! Ching!

If you found anything on this site of use, interesting, or even mildly amusing please consider tipping a few pennies in the jar to help look after our son, Georgi, to whom this blog is shamelessly dedicated.

Georgi contracted encephalitis and fell into a coma in the summer of 2007. He’s a strong boy and survived, but he suffered brain damage and still can’t walk or talk (well, not very well - but he does try). He’s growing all the time and every penny we collect goes towards his upkeep, and towards his future care requirements.

Thank you.