Re: How to strip lots of carriage returns (paragraph marker) in Word?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Greg Maxey (gmaxey_at_whamspammvps.org)
Date: 09/07/04


Date: Tue, 7 Sep 2004 15:27:36 -0400

Bruce,

I use this macro and function:

Sub CleanUpText()

Dim EP As Paragraph
Dim TextChar As String

If MsgBox("Do you want to remove leading spaces or characters?", vbYesNo) =
vbYes Then
  Call CUTReplaceSpecific("< {1,}*", "")
  Call CUTReplaceSpecific("(^13)( {1,}*)", "\1")
  Call CUTReplaceSpecific("(^l)( {1,}*)", "\1")
  Call CUTReplaceSpecific("( {1,})(^13)", "\2")
  Call CUTReplaceSpecific("( {1,})(^l)", "\2")
  Call CUTReplaceSpecific("([\>]{1,})( {1,})", "")
  Call CUTReplaceSpecific("(^l)([\> ]{1,})", "\1")
  Call CUTReplaceSpecific("(^13)([\> ]{1,})", "\1")
  Call CUTReplaceSpecific("(^l)([\<]{1,})", "\1")
  Call CUTReplaceSpecific("(^13)([\<]{1,})", "\1")
  Call CUTReplaceSpecific("(^13)( {1,})", "\1")
  Call CUTReplaceSpecific("(^l)( {1,})", "\1")
  Do
    TextChar = InputBox("Type in any additional leading character")
    ActiveDocument.Range(0, 0).Select
      With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Forward = True
        .Wrap = wdFindContinue
        .MatchWildcards = False
        .Text = TextChar
        .Replacement.Text = ""
        .Execute Replace:=wdReplaceAll
    End With
  Loop While TextChar > ""
Call CUTReplaceSpecific("< {1,}*", "")
End If

If MsgBox("Do you want to replace linebreaks with paragraph fromatting?",
vbYesNo) = vbYes Then
Call CUTReplaceSpecific("^l{2,}", "^p")
Call CUTReplaceSpecific("^l{1,}", " ")
End If

If MsgBox("Do you want to delete empty paragraphs in this document?",
vbYesNo) = vbYes Then
  For Each EP In ActiveDocument.Paragraphs
    If Len(EP.Range.Text) = 1 Then EP.Range.Delete
  Next EP
End If

End Sub
Function CUTReplaceSpecific(sSearchFor As String, sReplaceWith As String)
'CUT is Clean Up Text
ActiveDocument.Range(0, 0).Select
  With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Forward = True
    .Wrap = wdFindContinue
    .MatchWildcards = True
    .Text = sSearchFor
    .Replacement.Text = sReplaceWith
    .Execute Replace:=wdReplaceAll
  End With
End Function

-- 
Greg Maxey
A peer in "peer to peer" support
Rockledge, FL
To e-mail, edit out the "w...spam" in gmaxey@whamspammvps.org
BrucePT wrote:
> Importing or pasting text with embedded CRLFs creates ugly manual
> edit task. Any convenient way to edit/strip these in Word?  Other
> free/cheap utility? Can't find any paste special option, escape
> character, or editing options in Help or MS Web site that let me do
> this.
>
> Thanks, 


Relevant Pages

  • Re: Reading and processing text
    ... justifies it and writes it to another file. ... the strings with a space between them so a paragraph is one continuous ... string of words, eliminating having to deal with separate lines. ... input that consists of only words with alphabetic characters separated ...
    (comp.lang.lisp)
  • Re: automatic replacement of multi-lines text using macro ?
    ... >All the examples in the word documentation show only replacement of "simple" ... >text, one or more words, but not text on several lines (like a paragraph). ... A paragraph mark (looks like ¶ when nonprinting characters are ... displayed) is represented in the .Text string as vbCr or "^13". ...
    (microsoft.public.word.vba.general)
  • removing paragraph mark from selection
    ... I'm trying to add some characters before and after a selected string of text ... paragraph mark, even if one is part of the selected text. ... How can I make my script adjust the selection to exclude any paragraph ... Dim LineHeader As String, LineEnder As String, strTemp As String ...
    (microsoft.public.word.vba.general)
  • RE: Count the number of characters created by string text
    ... The len function does ... between characters, in a short paragraph created by a string text? ...
    (microsoft.public.excel)
  • Document Cleanup Automation
    ... I want to make apply the "Heading 1" style to the first paragraph and apply ... ' with more than 4 or 5 elements because you can't match the Find string ... ' Dim vFindText As Variant ... End Sub ...
    (microsoft.public.word.vba.general)