Remove spaces at end of paragraphs 
Author Message
 Remove spaces at end of paragraphs

Hi all,

I wanted to create a macro that removes all the extra spaces at the end of each paragraph in my
current selection. This is what I tried:

For Each Paragraph In Selection.Paragraphs
  Paragraph.Range.Text = RTrim(Paragraph.Range.Text)
Next

But this resulted in Word going into a never-ending loop, so I had to "ctrl+break" to stop it from
running. What is wrong with the code?

Thanks in advance,
Frederik



Sun, 29 Feb 2004 06:28:39 GMT  
 Remove spaces at end of paragraphs
Hi Frederik,

I think this has to do with the paragraph not changing in the selection, add
some .select statements and see what happens. Apart from that Word doesn't
seem to like to change the end of the paragraph and looses track ;-)
This code seems to do the trick:

Btw, one other thing, in this case probably unrelated but better to be save
then sorry, don't name your objectvariables the same as objects in Word,
oParagraph instead of Paragraph should save you trouble later on.

--------------------------------------------------
Dim oParagraph As Paragraph
Dim oRange As Range
Dim oRangePara As Range

  Set oParagraph = Selection.Paragraphs(1)
  Set oRange = Selection.Range

  Do Until oParagraph.Range.InRange(oRange.Paragraphs.Last.Range)
    Set oRangePara = oParagraph.Range
    oRangePara.MoveEnd unit:=wdCharacter, Count:=-1
    oRangePara.Text = RTrim(oRangePara.Text)
    Set oParagraph = oParagraph.Next
  Loop

  Set oRange = Nothing
  Set oRangePara = Nothing
  Set oParagraph = Nothing
--------------------------------------------------

Hope this helps,
regards,
Astrid

So that all can benefit from the discussion, please post all follow-ups to
the newsgroup.
Visit the MVP Word FAQ site at http://www.mvps.org/word/



Quote:
> Hi all,

> I wanted to create a macro that removes all the extra spaces at the end of

each paragraph in my
Quote:
> current selection. This is what I tried:

> For Each Paragraph In Selection.Paragraphs
>   Paragraph.Range.Text = RTrim(Paragraph.Range.Text)
> Next

> But this resulted in Word going into a never-ending loop, so I had to

"ctrl+break" to stop it from
Quote:
> running. What is wrong with the code?

> Thanks in advance,
> Frederik



Sun, 29 Feb 2004 07:28:40 GMT  
 Remove spaces at end of paragraphs
Hi Frederik

You don't need a macro for this. Just open the Edit Replace dialog. Type
"^w^p" (without the quotation marks) into the Find What box, and "^p" into
the "Replace With" box, and then click Replace All.

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
Word FAQs at http://www.multilinker.com/wordfaq
Please post any follow-up in the newsgroup. I do not reply to Word questions
by email


Quote:
> Hi all,

> I wanted to create a macro that removes all the extra spaces at the end of

each paragraph in my
Quote:
> current selection. This is what I tried:

> For Each Paragraph In Selection.Paragraphs
>   Paragraph.Range.Text = RTrim(Paragraph.Range.Text)
> Next

> But this resulted in Word going into a never-ending loop, so I had to

"ctrl+break" to stop it from
Quote:
> running. What is wrong with the code?

> Thanks in advance,
> Frederik



Sun, 29 Feb 2004 07:43:32 GMT  
 Remove spaces at end of paragraphs
Thanks Jonathan and Astrid,

both methods seem to work fast and correct.

Regards,
Frederik

Quote:
> You don't need a macro for this. Just open the Edit Replace dialog.
> Type "^w^p" (without the quotation marks) into the Find What box,
> and "^p" into the "Replace With" box, and then click Replace All.

-------------------------------------------
-------------------------------------------
Quote:
> Dim oParagraph As Paragraph
> Dim oRange As Range
> Dim oRangePara As Range
> Set oParagraph = Selection.Paragraphs(1)
> Set oRange = Selection.Range
> Do Until oParagraph.Range.InRange(oRange.Paragraphs.Last.Range)
> Set oRangePara = oParagraph.Range
> oRangePara.MoveEnd unit:=wdCharacter, Count:=-1
> oRangePara.Text = RTrim(oRangePara.Text)
> Set oParagraph = oParagraph.Next
> Loop
> Set oRange = Nothing
> Set oRangePara = Nothing
> Set oParagraph = Nothing



Sun, 29 Feb 2004 16:55:26 GMT  
 Remove spaces at end of paragraphs

Quote:

> You don't need a macro for this. Just open the Edit Replace dialog. Type
> "^w^p" (without the quotation marks) into the Find What box, and "^p" into
> the "Replace With" box, and then click Replace All.

This won't work on paragraphs in cells in tables, though. Always had
trouble with them.

Andrew
--
Andrew Gabb
[Syd/Canb 19-21 Sep - Ph: 02-626-51010, AH: 02 6297-5531]

phone: +61 8 8342-1021, fax: +61 8 8269-3280
-----



Thu, 04 Mar 2004 09:53:36 GMT  
 Remove spaces at end of paragraphs

Quote:


> > You don't need a macro for this. Just open the Edit Replace dialog. Type
> > "^w^p" (without the quotation marks) into the Find What box, and "^p" into
> > the "Replace With" box, and then click Replace All.

> This won't work on paragraphs in cells in tables, though. Always had
> trouble with them.

> Andrew

Hi Andrew --

Something like this might work for your purposes. Adaptable for Range
objects, and for looping through the document. Might need a 2nd pass
to target those pesky tables.

-- Michael

       With Selection
'Deselect and park the insertion point just after the para mark:
            .Collapse 0
'Move off the para mark:
            .MoveLeft
               With Selection
'Select contiguous spaces from that point leftwards and delete that
selection
                    .MoveStartWhile Cset:=" ", Count:=wdBackward
                   .Delete
                End With
        End With
'In case one or two spaces are still left standing, repeat for
insurance
        With Selection
            .MoveStartWhile Cset:=" ", Count:=wdBackward
            .Delete
        End With



Sat, 06 Mar 2004 03:58:06 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Frame-Remove Paragraph Spacing

2. (Q) Removing spaces at end of variable (MS Qbasic)

3. Removing spaces from the end of a string

4. reset character spacing to default paragraph spacing

5. End-of-Line vs. End-of-Paragraph

6. Space Space Space Space Space Space Space

7. Space Space Space Space Space Space Space

8. Space Space Space Space Space Space Space

9. disabling the space key or removing a space from a string

10. Need double spacing between paragraphs

11. Change a Style>Paragraph>line spacing value via VBA

12. Inserting a paragraph at end of document?

 

 
Powered by phpBB® Forum Software