Can this code be shortened

J

Joe Payne

I recently recorded a macro to remove unwanted symbols in a word 2003
document. The macro works fine but I am wondering if the code couldn't
be shortened. I used cut and paste to add new code for each symbol I
wanted to remove. Is there a way to shorten this code and still remove
all the symbols. Below is just some of the code contained in the
macro.
------------------------------------------------------------------------------------------------------------------

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 8/14/2005 by Joe Payne
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ">"
.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
Selection.Find.Execute
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "*"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "@"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "("
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = ")"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "$"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
 
K

Karl E. Peterson

Hi Joe --

This is a classic case of needing to write a helper subroutine, as you're pretty much
executing the same code over and over. Something like this (warning: air code! I
have *absolutely no idea* about the exact syntax for that .Find thingie you're
using...):

Public Sub ReplaceText(SearchFor As String, Optional ReplaceWith As String = "")
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Execute
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = SearchFor
.Replacement.Text = ReplaceWith
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
End Sub

And call it repeatedly with each symbol, something like this:

MySymbols = ">*@()$"
For i = 1 To Len(MySymbols)
Call ReplaceText(Mid$(MySymbols, i, 1))
Next i

Sound reasonable?

Later... Karl
 
R

RB Smissaert

Try this:


Sub test()

Dim i As Long
Dim strFind As String
Dim strReplace As String

For i = 1 To 6
strFind = Choose(i, ">", "*", "@", "(", ")", "$")
strReplace = Choose(i, "", "", "", "", "", "")
With Selection.Find
.Text = strFind
.Replacement.Text = strReplace
.Execute Replace:=wdReplaceAll
End With
Next

End Sub


RBS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top