Skip to content

Instantly share code, notes, and snippets.

@DBremen
Created August 9, 2018 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DBremen/36d2816fff59537486dd1c292a707580 to your computer and use it in GitHub Desktop.
Save DBremen/36d2816fff59537486dd1c292a707580 to your computer and use it in GitHub Desktop.
Word Macro to cleanup text copied from eBooks after pasted into MS Word
Sub RemoveExtraSpaces()
Selection.InsertAfter "®®"
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^p"
'What to replace it with
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = False
.Execute Replace:=wdReplaceAll
'multi spaces with single space
.Text = " [ ]@([! ])"
.Replacement.Text = " \1"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
'hyphenation
.Text = "- "
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
.Text = "®®"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = False
.Execute Replace:=wdReplaceAll
'space + dot
.Text = " ."
.Replacement.Text = "."
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
Selection.Style = ActiveDocument.Styles("Normal")
End With
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment