Home Code Format text Justia.com MS Word macro remove line breaks

Format text Justia.com MS Word macro remove line breaks

1663
0

One of my users was pulling some text from a legal website (www.justia.com) and asked me if I could help her paste it into MS Word.  I thought it would be as simple as paste special to delete the formatting, but that doesn’t really cut it here (pun intended).  Pasting as formatted text(RTF) is the only one which removed the line breaks, but you still have to add the line breaks back in for each section if you paste multiple sections.

I immediately thought about a macro, but that’s overkill right? Right?  At any rate, you can use Replace in MS Word to easily clean up your text:

Step 1: Replace all double line breaks with a special character or characters.  Line breaks are represented by ^p so a double line break would be ^p^p.  I used *** as the text to replace the breaks with because there were no other occurrences of *** in the text.
Step 2: Replace all single line breaks with a space.
Step 3: Replace all sets of four spaces with a line break and four spaces
Step 4: Replace all sets of *** with a line break

Or, if you will be copy/pasting from this site often, you could insert this code into a new macro in MS Word:

Attribute VB_Name = “NewMacros”
Sub Cleanup_Justia()
Attribute Cleanup_Justia.VB_Description = “Cleans up and reformats the text from www.justia.com when pasted into a Word doc”
Attribute Cleanup_Justia.VB_ProcData.VB_Invoke_Func = “Normal.NewMacros.Cleanup_Justia”

‘ Cleanup_Justia Macro
‘ Cleans up and reformats the text from www.justia.com when pasted into a Word doc

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = “^p^p”
        .Replacement.Text = “***”
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    With Selection.Find
        .Text = “^p”
        .Replacement.Text = ” ”
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    With Selection.Find
        .Text = ”    ”
        .Replacement.Text = “^p    ”
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    With Selection.Find
        .Text = “***”
        .Replacement.Text = “^p”
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub

LEAVE A REPLY

Please enter your comment!
Please enter your name here