Searching for and Replacing Multiple items in a macro

S

snsd

Hi:

There is a financial messaging service known as SWIFT. SWIFT does not allow
the usage of certain characters in its messages. I am attempting to create
some code that will strip a Word document of the invalid characters – and
preferably replace the invalid character with a highlighted space so the user
can easily identify what the document will look like without the invalid
characters. The following code successfully removes the “*†character from my
document. I would like to modify the code so that it will search for a list
of other characters such as &, $, %, #, etc. and remove them from the
document as well. Ideally, I would like the space where the character was
removed to be highlighted so the user can see where the character was
removed. (I am fine replacing the character with the “space†character.) Is
there a way to search for and replace multiple characters without having to
repeat the code multiple times? I haven’t done a lot of programming in Word –
but have a basic understanding of VBA in an Access environment. Any help
would be greatly appreciated.

Thanks,

Dave

(The following code was created using the macro recorder in Word.)

Sub RemoveInvalidISO15022characters()
' Removes Invalid ISO15022 characters from document

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
End Sub
 
G

Greg Maxey

Try:

Sub ScratchMacro()
Dim oRng As Word.Range
Dim pList() As String
Dim i As Long
pList() = Split("#,$,%,&,*", ",")
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
For i = 0 To UBound(pList)
With oRng.Find
.Text = pList(i)
.Replacement.Text = " "
.Replacement.Highlight = True
.Forward = True
.Execute Replace:=wdReplaceAll
End With
Next i
End Sub
 
G

Graham Mayor

The following will replace a list of characters each in quotes and separated
by commas as below with a green highlighted space:

Sub ReplaceList()
Dim vFindText As Variant
Dim sReplText As String
Dim sHighlight As String
Dim i As Long

sHighlight = Options.DefaultHighlightColorIndex
Options.DefaultHighlightColorIndex = wdBrightGreen
vFindText = Array("*", "&", "$", "%", "#")
sReplText = " "
With Selection.Find
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Format = True
.MatchCase = True
For i = LBound(vFindText) To UBound(vFindText)
.Text = vFindText(i)
.Replacement.Text = sReplText
.Replacement.Highlight = True
.Execute replace:=wdReplaceAll
Next i
End With
Options.DefaultHighlightColorIndex = sHighlight
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
H

Helmut Weber

Hi,

may I add my own decent bit:

Sub Test501()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
Options.DefaultHighlightColorIndex = wdYellow
With rDcm.Find
.Text = "[&$%#]{1}"
.Replacement.Text = " "
.Replacement.Highlight = True
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

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