Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word: Macro to Suppress Leading/Trailing Blanks 3

Status
Not open for further replies.

Pleonasm

Technical User
May 20, 2004
121
CA
I am seeking a mechanism (perhaps a macro?) to strip away any leading or trailing blanks from each line of text in a Microsoft Word 2003 document.

Your recommendations are appreciated.
 
If there are always at least two spaces before and after the text and the text inbetween there would never be more than one space you could do a global using wildcards (something I've learned on this site before, thanks to Tony Jollans).
Ctrl + H to begin your global.
Click on More to bring up entire Find and Replace menu.
Click on Use Wildcards
Type two spaces in Find
After the two spaces type {1,} (those braces are above the brackets)
This will remove all groups of spaces as long as there are at least two spaces.
 
An alternate approach, to strip away leading/trailing spaces at paragraph boundaries without affecting spacing within a paragraph, is to run two Find/Replace procedures - one to replace para markers preceded by spaces with just the para marker, and one to replace para markers followed by spaces with just the para marker. To do this:
. Begin your Find/Replace procedure (eg via Ctrl-H).
. Type a single space followed by '^p' in the Find box
. Type '^p' in the Replace box.
. Execute the process. You may want to do this more than once, until Word reports no replacements.
. Type '^p' followed by in a single space the Find box.
. Execute the process. Again, you may want to do this more than once, until Word reports no replacements.

The same approach can be used for clearing out unwanted spaces before/after tabs (using '^t) and soft returns (using ^l).

Cheers
 

A simple way to remove leading and trailing spaces is to centre the lines and then left justify them again ...

Ctrl+a, Ctrl+e, Ctrl+l

(to do it to a complete document)

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
All suggestions are good. I would like to point out however:

1. BenRowe's suggestion would also remove two spaces within the line. So if the text has two leading spaces at the start of a sentence (still not uncommon, although a lot of text uses a single space leading into a sentence), those would be removed. The sentence break would have no spaces.

2. Tony's suggestion removes ALL spaces regardless of count. So if the criteria was explicitly TWO spaces (both leading and trailing), but NOT one space (or three for that matter), then it does not meet the requirement.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top