G
Greg
I have been working on a macro to simplify cleaning up
text pasted from the web. In the process I found my code
had grown larger and larger due mainly to repetitive
steps. I stumbled on a feature called Case and managed to
apply it which greatly reduced the clutter in my code.
While it is working, I was wondering if someone would take
a peek and let me know if I have used Case in a method for
which is was intended. Thanks
Sub CleanUpText()
Dim EP As Paragraph
Dim TextChar As String
Dim i As Long
i = 1
If MsgBox("Do you want to remove leading spaces or _
characters?", vbYesNo) = vbYes Then
ActiveDocument.Range(0, 0).Select
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
For i = 1 To 14
Select Case i
Case 1
.Text = "< {1,}*"
.Replacement.Text = ""
Case 2
.Text = "(^13)( {1,}*)"
.Replacement.Text = "\1"
Case 3
.Text = "(^l)( {1,}*)"
.Replacement.Text = "\1"
Case 4
.Text = "( {1,})(^13)"
.Replacement.Text = "\2"
Case 5
.Text = "( {1,})(^l)"
.Replacement.Text = "\2"
Case 6
.Text = "([\>]{1,})( {1,})"
.Replacement.Text = ""
Case 7
.Text = "(^l)([\> ]{1,})"
.Replacement.Text = "\1"
Case 8
.Text = "(^13)([\> ]{1,})"
.Replacement.Text = "\1"
Case 9
.Text = "(^l)([\<]{1,})"
.Replacement.Text = "\1"
Case 10
.Text = "(^13)([\<]{1,})"
.Replacement.Text = "\1"
Case 11
.Text = "(^13)( {1,})"
.Replacement.Text = "\1"
Case 12
.Text = "(^l)( {1,})"
.Replacement.Text = "\1"
Case 13
Do
TextChar = InputBox("Type in any additional " & _
"leading character")
.MatchWildcards = False
.Text = TextChar
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
Loop While TextChar > ""
Case 14
.MatchWildcards = True
.Text = "< {1,}*"
.Replacement.Text = ""
Case Else
Exit For
End Select
.Execute Replace:=wdReplaceAll
Next
End With
End If
i = 1
If MsgBox("Do you want to replace linebreaks " & _
with paragraph fromatting?", vbYesNo) = vbYes Then
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
For i = 1 To 2
Select Case i
Case 1
.Text = "^l{2,}"
.Replacement.Text = "^p"
Case 2
.Text = "^l{1,}"
.Replacement.Text = " "
Case Else
Exit For
End Select
.Execute Replace:=wdReplaceAll
Next
End With
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
text pasted from the web. In the process I found my code
had grown larger and larger due mainly to repetitive
steps. I stumbled on a feature called Case and managed to
apply it which greatly reduced the clutter in my code.
While it is working, I was wondering if someone would take
a peek and let me know if I have used Case in a method for
which is was intended. Thanks
Sub CleanUpText()
Dim EP As Paragraph
Dim TextChar As String
Dim i As Long
i = 1
If MsgBox("Do you want to remove leading spaces or _
characters?", vbYesNo) = vbYes Then
ActiveDocument.Range(0, 0).Select
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
For i = 1 To 14
Select Case i
Case 1
.Text = "< {1,}*"
.Replacement.Text = ""
Case 2
.Text = "(^13)( {1,}*)"
.Replacement.Text = "\1"
Case 3
.Text = "(^l)( {1,}*)"
.Replacement.Text = "\1"
Case 4
.Text = "( {1,})(^13)"
.Replacement.Text = "\2"
Case 5
.Text = "( {1,})(^l)"
.Replacement.Text = "\2"
Case 6
.Text = "([\>]{1,})( {1,})"
.Replacement.Text = ""
Case 7
.Text = "(^l)([\> ]{1,})"
.Replacement.Text = "\1"
Case 8
.Text = "(^13)([\> ]{1,})"
.Replacement.Text = "\1"
Case 9
.Text = "(^l)([\<]{1,})"
.Replacement.Text = "\1"
Case 10
.Text = "(^13)([\<]{1,})"
.Replacement.Text = "\1"
Case 11
.Text = "(^13)( {1,})"
.Replacement.Text = "\1"
Case 12
.Text = "(^l)( {1,})"
.Replacement.Text = "\1"
Case 13
Do
TextChar = InputBox("Type in any additional " & _
"leading character")
.MatchWildcards = False
.Text = TextChar
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
Loop While TextChar > ""
Case 14
.MatchWildcards = True
.Text = "< {1,}*"
.Replacement.Text = ""
Case Else
Exit For
End Select
.Execute Replace:=wdReplaceAll
Next
End With
End If
i = 1
If MsgBox("Do you want to replace linebreaks " & _
with paragraph fromatting?", vbYesNo) = vbYes Then
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
For i = 1 To 2
Select Case i
Case 1
.Text = "^l{2,}"
.Replacement.Text = "^p"
Case 2
.Text = "^l{1,}"
.Replacement.Text = " "
Case Else
Exit For
End Select
.Execute Replace:=wdReplaceAll
Next
End With
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