# VBA macro for word doc that removes space at the end



## motivation (Apr 4, 2006)

How can I remove the space from the end of lines in a word document ?

I need to read the file and save it ..

I saw the RTrim , but I didn't know how to use it ?



Thanks


----------



## Rockn (Jul 29, 2001)

Why?? You would have to loop through every line, turn each line into a string, trim off the spaces and write them back to the screen.


----------



## MWE (Mar 21, 2005)

motivation said:


> How can I remove the space from the end of lines in a word document ?
> 
> I need to read the file and save it ..
> 
> ...


This is pretty easy to do using VBA, but could you be a little clearer re you want you really want to do? It would also help if you could explain why you want to do this

do you wish to trim off spaces at the end of a line but leave line control characters
or just trim line control characters (for example line feeds and similar)
or both
what do you want to do with "new paragraph" controls
if you remove line control characters you will end up with a single string of words for each paragraph and if you also remove paragraph controls you will end up with a single string of words for the whole file.


----------



## Cyps (Jan 3, 2006)

You said you want to remove spaces at the end of lines. Am I right in thinking you have short lines like the example below and want to remove the formatting so the lines run the full width of a page? So that : -

The quick brown fox jumps 
over the lazy dog. The quick 
brown fox jumps over the lazy
dog.

Becomes: -

The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.

If so here is the macro which will do that: -

Sub StripParagraphs()
For x = 1 To 30
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p^p^p"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll
Next x
End Sub


You can see that this macro is set to 30 lines just change this number to suit your purposes. Apologies if I have misunderstood you but it did seem like this is what you wanted.


----------

