Word

Eliminate Multiple Spaces

Ease of Use

Easy

Version tested with

2000 

Submitted by:

lucas

Description:

Reduces excessive spaces between words to one space 

Discussion:

You have imported text or pasted it into your document or for whatever reason, you have more than one space between a lot of the words. This routine will reduce those excessive spaces to one space. 

Code:

instructions for use

			

Place this code In a standard module: Option Explicit Sub EliminateMultipleSpaces() 'If something goes wrong, go to the errorhandler On Error GoTo ERRORHANDLER 'Checks the document for excessive spaces between words With Selection .HomeKey Unit:=wdStory With .Find .ClearFormatting .Replacement.ClearFormatting 'Here is where it is actually looking for spaces between words .Text = " [ ]@([! ])" 'This line tells it to replace the excessive spaces with one space .Replacement.Text = " \1" .MatchWildcards = True .Wrap = wdFindStop .Format = False .Forward = True 'execute the replace .Execute Replace:=wdReplaceAll End With With .Find 'This time its looking for excessive spaces after a paragraph mark .Text = "^p " 'What to replace it with .Replacement.Text = "^p" .MatchWildcards = False .Wrap = wdFindStop .Format = False .Forward = True 'Execute the replace .Execute Replace:=wdReplaceAll End With End With ERRORHANDLER: With Selection .ExtendMode = False .HomeKey Unit:=wdStory End With End Sub

How to use:

  1. Open the Visual Basic Editor by going to tools-Macro's-Visual Basic Editor or use Alt-F11
  2. On the toolbar of the Visual Basic Editor, go to insert - module
  3. In the module pane paste the code above.
  4. Close the Visual Basic Editor by clicking the X in the upper right corner or go to File-Close
 

Test the code:

  1. Type a sentence into your document and add extra spaces between the words.
  2. On the main menu go to tools-macro-macros.
  3. In the dialog window select EliminateMultipleSpaces and then click run.
  4. The excessive spaces will be reduced to one space.
 

Sample File:

EliminateMultipleSpaces.zip 9.66KB 

Approved by mdmackillop


This entry has been viewed 159 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express