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