View Single Post
 
Old 02-06-2016, 02:09 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 20,060
macropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant future
Default Cleaning up Text Pasted from Websites, E-mails, PDFs etc.

When you paste text from a PDF, web site or an email, you may end up with a paragraph break at the end of every line within a logical paragraph, and two such breaks between logical paragraphs. Such text stubbornly refuses to honour justification, for example, because there's nothing to justify - it's all a series of one-line paragraphs. You should be able to see this if you have Word configured to display formatting marks on-screen. Clicking the symbol on the toolbar/home tab toggles this on/off.

The following series of wildcard Find/Replace actions cleans up text pasted from emails, websites, etc., that insert paragraph breaks at the end of every line. Note also that the process assumes there are at least two such paragraph breaks between the 'real' paragraphs.

To do a wildcard Find/Replace, open the Find/Replace dialogue, then click 'More' and click on the 'use wildcards' option.

Find = [ ^s^t]{1,}^13
Replace = ^p
Find = ([!^13^l])([^13^l])([!^13^l])
Replace = \1 \3
Find = [^s ]{2,}
Replace = ^32
Find = ([a-z])-[ ^s]{1,}([a-z])
Replace = \1\2
Find = [^13^l]{1,}
Replace = ^p

Note: Depending on your system's regional settings, you may need to replace all the commas in the above Find/Replace expressions with semi-colons. For example:
[ ^s^t]{1,}^13
becomes:
[ ^s^t]{1;}^13

The following macro automates the above Find/Replace sequence, as well as dealing with any internationalisation issues.
Code:
Sub CleanUpPastedText()
'Turn Off Screen Updating
Application.ScreenUpdating = False
Dim StrFnd As String
With ActiveDocument.Range.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Forward = True
  .Wrap = wdFindStop
  .Format = False
  .MatchAllWordForms = False
  .MatchSoundsLike = False
  .MatchWildcards = True
  'Eliminate spaces & tabs before paragraph breaks.
  StrFnd = "[ ^s^t]{1,}^13"
  'Edit StrFnd as needed for internationalisation issues
  If Application.International(wdListSeparator) = ";" Then
    StrFnd = Replace(StrFnd, ",", ";")
  End If
  .Text = StrFnd
  .Replacement.Text = "^p"
  .Execute Replace:=wdReplaceAll
  'Replace single paragraph breaks and line breaks with a space
  .Text = "([!^13^l])([^13^l])([!^13^l])"
  .Replacement.Text = "\1 \3"
  .Execute Replace:=wdReplaceAll
  'Replace all double spaces with single spaces
  StrFnd = "[^s ]{2,}"
  'Edit StrFnd as needed for internationalisation issues
  If Application.International(wdListSeparator) = ";" Then
    StrFnd = Replace(StrFnd, ",", ";")
  End If
  .Text = StrFnd
  .Replacement.Text = " "
  .Execute Replace:=wdReplaceAll
  'Delete hypens in hyphenated text formerly split across lines
  StrFnd = "([a-z])-[^s ]{1,}([a-z])"
  'Edit StrFnd as needed for internationalisation issues
  If Application.International(wdListSeparator) = ";" Then
    StrFnd = Replace(StrFnd, ",", ";")
  End If
  .Text = StrFnd
  .Replacement.Text = "\1\2"
  .Execute Replace:=wdReplaceAll
  'Limit paragraph breaks and line breaks to one paragraph break per 'real' paragraph.
  StrFnd = "[^13^l]{1,}"
  'Edit StrFnd as needed for internationalisation issues
  If Application.International(wdListSeparator) = ";" Then
    StrFnd = Replace(StrFnd, ",", ";")
  End If
  .Text = StrFnd
  .Replacement.Text = "^p"
  .Execute Replace:=wdReplaceAll
End With
'Restore Screen Updating
Application.ScreenUpdating = True
End Sub
For PC macro installation & usage instructions, see: http://www.gmayor.com/installing_macro.htm
For Mac macro installation & usage instructions, see: http://word.mvps.org/Mac/InstallMacro.html

If you'd prefer to run the macro against just a selected range, change:
ActiveDocument
to:
Selection
__________________
Cheers,
Paul Edstein
[MS MVP - Word]