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

Re: Searching for and Replacing Multiple items in a macro by Greg

Greg
Thu Aug 03 06:58:06 CDT 2006

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


snsd wrote:
> 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


Re: Searching for and Replacing Multiple items in a macro by Graham

Graham
Thu Aug 03 07:33:34 CDT 2006

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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

snsd wrote:
> 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



Re: Searching for and Replacing Multiple items in a macro by Helmut

Helmut
Thu Aug 03 09:48:41 CDT 2006

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"

Re: Searching for and Replacing Multiple items in a macro by snsd

snsd
Fri Aug 04 09:46:02 CDT 2006

Thanks to all of you for your excellent responses. They were all very helpful
- and they work! I have one more question that I should have thought of
beforehand. I'm assuming it's a simple adjustment to the code. I've realized
that rather than having a list of INVALID characters, it would be simpler to
have a list of VALID characters. The valid characters are:

ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
0123456789
/:-?()+.,'

So, I would like to remove any characters that do NOT match the above
characters. I realized that when you get into all the non-keyboard
characters...that my list would be very difficult to manage. Any help in
modifying the code to replace all characters NOT matching the above would be
great.

Thanks,

Dave

"Graham Mayor" wrote:

> 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
>
> My web site www.gmayor.com
> Word MVP web site http://word.mvps.org
> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
>
> snsd wrote:
> > 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
>
>
>

Re: Searching for and Replacing Multiple items in a macro by Dave

Dave
Fri Aug 04 09:58:28 CDT 2006

Hi,

The following works on my machine.

With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.MatchWildcards = True
.Text = "[!A-z0-9/:/(/)+.,' ^13-\?]"
With .Replacement
.ClearFormatting
.Text = ""
End With

.Execute Replace:=wdReplaceAll
End With
End With

HTH,
Dave

"snsd" <snsd@discussions.microsoft.com> wrote in message
news:33E58823-5018-4CAA-A2CF-D06C23CD11FE@microsoft.com...
> Thanks to all of you for your excellent responses. They were all very
> helpful
> - and they work! I have one more question that I should have thought of
> beforehand. I'm assuming it's a simple adjustment to the code. I've
> realized
> that rather than having a list of INVALID characters, it would be simpler
> to
> have a list of VALID characters. The valid characters are:
>
> ABCDEFGHIJKLMNOPQRSTUVWXYZ
> abcdefghijklmnopqrstuvwxyz
> 0123456789
> /:-?()+.,'
>
> So, I would like to remove any characters that do NOT match the above
> characters. I realized that when you get into all the non-keyboard
> characters...that my list would be very difficult to manage. Any help in
> modifying the code to replace all characters NOT matching the above would
> be
> great.
>
> Thanks,
>
> Dave
>
> "Graham Mayor" wrote:
>
>> 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
>>
>> My web site www.gmayor.com
>> Word MVP web site http://word.mvps.org
>> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
>>
>> snsd wrote:
>> > 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
>>
>>
>>



Re: Searching for and Replacing Multiple items in a macro by Greg

Greg
Fri Aug 04 10:08:47 CDT 2006

In this case Helmut was J... come late, but provided the better
solution ;-)


I assumed that you didn't want spaces highlighted.

Try:
Sub Test501()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
Options.DefaultHighlightColorIndex = wdYellow
With rDcm.Find
.Text = "[!A-Za-z0-9 ^13:'+.,-///?/(/)]"
.Replacement.Text = " "
.Replacement.Highlight = True
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub





snsd wrote:
> Thanks to all of you for your excellent responses. They were all very helpful
> - and they work! I have one more question that I should have thought of
> beforehand. I'm assuming it's a simple adjustment to the code. I've realized
> that rather than having a list of INVALID characters, it would be simpler to
> have a list of VALID characters. The valid characters are:
>
> ABCDEFGHIJKLMNOPQRSTUVWXYZ
> abcdefghijklmnopqrstuvwxyz
> 0123456789
> /:-?()+.,'
>
> So, I would like to remove any characters that do NOT match the above
> characters. I realized that when you get into all the non-keyboard
> characters...that my list would be very difficult to manage. Any help in
> modifying the code to replace all characters NOT matching the above would be
> great.
>
> Thanks,
>
> Dave
>
> "Graham Mayor" wrote:
>
> > 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
> >
> > My web site www.gmayor.com
> > Word MVP web site http://word.mvps.org
> > <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> >
> > snsd wrote:
> > > 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
> >
> >
> >


Re: Searching for and Replacing Multiple items in a macro by snsd

snsd
Fri Aug 04 10:25:02 CDT 2006

Thanks Dave. Sincerely appreciated. I tried your code as follows. (I am NOT a
programmer so don't entirely understand the logic of what you've done.)

Sub ReplaceList()

With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.MatchWildcards = True
.Text = "[!A-z0-9/:/(/)+.,' ^13-\?]"
With .Replacement
.ClearFormatting
.Text = ""
End With

.Execute Replace:=wdReplaceAll
End With
End With

End Sub

The only characters outside of my list that it removed were ~, @ and {}. It
didn't remove any other characters that I would have expected it to. Any idea
as to why that might be happening?

Thanks,

Dave

"Dave Lett" wrote:

> Hi,
>
> The following works on my machine.
>
> With Selection
> .HomeKey Unit:=wdStory
> With .Find
> .ClearFormatting
> .MatchWildcards = True
> .Text = "[!A-z0-9/:/(/)+.,' ^13-\?]"
> With .Replacement
> .ClearFormatting
> .Text = ""
> End With
>
> .Execute Replace:=wdReplaceAll
> End With
> End With
>
> HTH,
> Dave
>
> "snsd" <snsd@discussions.microsoft.com> wrote in message
> news:33E58823-5018-4CAA-A2CF-D06C23CD11FE@microsoft.com...
> > Thanks to all of you for your excellent responses. They were all very
> > helpful
> > - and they work! I have one more question that I should have thought of
> > beforehand. I'm assuming it's a simple adjustment to the code. I've
> > realized
> > that rather than having a list of INVALID characters, it would be simpler
> > to
> > have a list of VALID characters. The valid characters are:
> >
> > ABCDEFGHIJKLMNOPQRSTUVWXYZ
> > abcdefghijklmnopqrstuvwxyz
> > 0123456789
> > /:-?()+.,'
> >
> > So, I would like to remove any characters that do NOT match the above
> > characters. I realized that when you get into all the non-keyboard
> > characters...that my list would be very difficult to manage. Any help in
> > modifying the code to replace all characters NOT matching the above would
> > be
> > great.
> >
> > Thanks,
> >
> > Dave
> >
> > "Graham Mayor" wrote:
> >
> >> 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
> >>
> >> My web site www.gmayor.com
> >> Word MVP web site http://word.mvps.org
> >> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> >>
> >> snsd wrote:
> >> > 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
> >>
> >>
> >>
>
>
>

Re: Searching for and Replacing Multiple items in a macro by Greg

Greg
Fri Aug 04 11:05:39 CDT 2006

I didn't test the code, but part of the problem is explained by using
!A-z.

!A-z would be expected to exclude all the letters between A and z i.e.
both upper case and lower case letters, which it does, but it excludes
all the characters in the ASCII 65 to ASCII 122 range, and that block
includes the characters [ ] ` ^ _ /.

Use !A-Za-z instead.

snsd wrote:
> Thanks Dave. Sincerely appreciated. I tried your code as follows. (I am NOT a
> programmer so don't entirely understand the logic of what you've done.)
>
> Sub ReplaceList()
>
> With Selection
> .HomeKey Unit:=wdStory
> With .Find
> .ClearFormatting
> .MatchWildcards = True
> .Text = "[!A-z0-9/:/(/)+.,' ^13-\?]"
> With .Replacement
> .ClearFormatting
> .Text = ""
> End With
>
> .Execute Replace:=wdReplaceAll
> End With
> End With
>
> End Sub
>
> The only characters outside of my list that it removed were ~, @ and {}. It
> didn't remove any other characters that I would have expected it to. Any idea
> as to why that might be happening?
>
> Thanks,
>
> Dave
>
> "Dave Lett" wrote:
>
> > Hi,
> >
> > The following works on my machine.
> >
> > With Selection
> > .HomeKey Unit:=wdStory
> > With .Find
> > .ClearFormatting
> > .MatchWildcards = True
> > .Text = "[!A-z0-9/:/(/)+.,' ^13-\?]"
> > With .Replacement
> > .ClearFormatting
> > .Text = ""
> > End With
> >
> > .Execute Replace:=wdReplaceAll
> > End With
> > End With
> >
> > HTH,
> > Dave
> >
> > "snsd" <snsd@discussions.microsoft.com> wrote in message
> > news:33E58823-5018-4CAA-A2CF-D06C23CD11FE@microsoft.com...
> > > Thanks to all of you for your excellent responses. They were all very
> > > helpful
> > > - and they work! I have one more question that I should have thought of
> > > beforehand. I'm assuming it's a simple adjustment to the code. I've
> > > realized
> > > that rather than having a list of INVALID characters, it would be simpler
> > > to
> > > have a list of VALID characters. The valid characters are:
> > >
> > > ABCDEFGHIJKLMNOPQRSTUVWXYZ
> > > abcdefghijklmnopqrstuvwxyz
> > > 0123456789
> > > /:-?()+.,'
> > >
> > > So, I would like to remove any characters that do NOT match the above
> > > characters. I realized that when you get into all the non-keyboard
> > > characters...that my list would be very difficult to manage. Any help in
> > > modifying the code to replace all characters NOT matching the above would
> > > be
> > > great.
> > >
> > > Thanks,
> > >
> > > Dave
> > >
> > > "Graham Mayor" wrote:
> > >
> > >> 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
> > >>
> > >> My web site www.gmayor.com
> > >> Word MVP web site http://word.mvps.org
> > >> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> > >>
> > >> snsd wrote:
> > >> > 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
> > >>
> > >>
> > >>
> >
> >
> >


Re: Searching for and Replacing Multiple items in a macro by snsd

snsd
Fri Aug 04 11:34:02 CDT 2006

Greg: Thanks for the suggestion. VERY close to working. I think the ' ^13- is
the challenge. I'm assuming that is an attempt to identify a '. When I take
it out and use your suggestion, all works perfectly except that the ' gets
removed when it shouldn't.

What should be put in to leave ' in the list?

Thanks,
Dave

"Greg Maxey" wrote:

> I didn't test the code, but part of the problem is explained by using
> !A-z.
>
> !A-z would be expected to exclude all the letters between A and z i.e.
> both upper case and lower case letters, which it does, but it excludes
> all the characters in the ASCII 65 to ASCII 122 range, and that block
> includes the characters [ ] ` ^ _ /.
>
> Use !A-Za-z instead.
>
> snsd wrote:
> > Thanks Dave. Sincerely appreciated. I tried your code as follows. (I am NOT a
> > programmer so don't entirely understand the logic of what you've done.)
> >
> > Sub ReplaceList()
> >
> > With Selection
> > .HomeKey Unit:=wdStory
> > With .Find
> > .ClearFormatting
> > .MatchWildcards = True
> > .Text = "[!A-z0-9/:/(/)+.,' ^13-\?]"
> > With .Replacement
> > .ClearFormatting
> > .Text = ""
> > End With
> >
> > .Execute Replace:=wdReplaceAll
> > End With
> > End With
> >
> > End Sub
> >
> > The only characters outside of my list that it removed were ~, @ and {}. It
> > didn't remove any other characters that I would have expected it to. Any idea
> > as to why that might be happening?
> >
> > Thanks,
> >
> > Dave
> >
> > "Dave Lett" wrote:
> >
> > > Hi,
> > >
> > > The following works on my machine.
> > >
> > > With Selection
> > > .HomeKey Unit:=wdStory
> > > With .Find
> > > .ClearFormatting
> > > .MatchWildcards = True
> > > .Text = "[!A-z0-9/:/(/)+.,' ^13-\?]"
> > > With .Replacement
> > > .ClearFormatting
> > > .Text = ""
> > > End With
> > >
> > > .Execute Replace:=wdReplaceAll
> > > End With
> > > End With
> > >
> > > HTH,
> > > Dave
> > >
> > > "snsd" <snsd@discussions.microsoft.com> wrote in message
> > > news:33E58823-5018-4CAA-A2CF-D06C23CD11FE@microsoft.com...
> > > > Thanks to all of you for your excellent responses. They were all very
> > > > helpful
> > > > - and they work! I have one more question that I should have thought of
> > > > beforehand. I'm assuming it's a simple adjustment to the code. I've
> > > > realized
> > > > that rather than having a list of INVALID characters, it would be simpler
> > > > to
> > > > have a list of VALID characters. The valid characters are:
> > > >
> > > > ABCDEFGHIJKLMNOPQRSTUVWXYZ
> > > > abcdefghijklmnopqrstuvwxyz
> > > > 0123456789
> > > > /:-?()+.,'
> > > >
> > > > So, I would like to remove any characters that do NOT match the above
> > > > characters. I realized that when you get into all the non-keyboard
> > > > characters...that my list would be very difficult to manage. Any help in
> > > > modifying the code to replace all characters NOT matching the above would
> > > > be
> > > > great.
> > > >
> > > > Thanks,
> > > >
> > > > Dave
> > > >
> > > > "Graham Mayor" wrote:
> > > >
> > > >> 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
> > > >>
> > > >> My web site www.gmayor.com
> > > >> Word MVP web site http://word.mvps.org
> > > >> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> > > >>
> > > >> snsd wrote:
> > > >> > 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
> > > >>
> > > >>
> > > >>
> > >
> > >
> > >
>
>

Re: Searching for and Replacing Multiple items in a macro by Dave

Dave
Fri Aug 04 11:38:16 CDT 2006

Hi Greg,

If using !A-z also excludes characters in the ASCII 65 to ASCII 122 range,
then perhaps some MVP could push to have the article "Finding and replacing
characters using wildcards" on the MVPs Web site. It includes the following:

[0-9A-z] will find any numbers or letters.


thanks for drawing my attention to this.
Dave

"Greg Maxey" <gmaxey@mvps.org> wrote in message
news:1154707539.587933.62260@m73g2000cwd.googlegroups.com...
>I didn't test the code, but part of the problem is explained by using
> !A-z.
>
> !A-z would be expected to exclude all the letters between A and z i.e.
> both upper case and lower case letters, which it does, but it excludes
> all the characters in the ASCII 65 to ASCII 122 range, and that block
> includes the characters [ ] ` ^ _ /.
>
> Use !A-Za-z instead.
>
> snsd wrote:
>> Thanks Dave. Sincerely appreciated. I tried your code as follows. (I am
>> NOT a
>> programmer so don't entirely understand the logic of what you've done.)
>>
>> Sub ReplaceList()
>>
>> With Selection
>> .HomeKey Unit:=wdStory
>> With .Find
>> .ClearFormatting
>> .MatchWildcards = True
>> .Text = "[!A-z0-9/:/(/)+.,' ^13-\?]"
>> With .Replacement
>> .ClearFormatting
>> .Text = ""
>> End With
>>
>> .Execute Replace:=wdReplaceAll
>> End With
>> End With
>>
>> End Sub
>>
>> The only characters outside of my list that it removed were ~, @ and {}.
>> It
>> didn't remove any other characters that I would have expected it to. Any
>> idea
>> as to why that might be happening?
>>
>> Thanks,
>>
>> Dave
>>
>> "Dave Lett" wrote:
>>
>> > Hi,
>> >
>> > The following works on my machine.
>> >
>> > With Selection
>> > .HomeKey Unit:=wdStory
>> > With .Find
>> > .ClearFormatting
>> > .MatchWildcards = True
>> > .Text = "[!A-z0-9/:/(/)+.,' ^13-\?]"
>> > With .Replacement
>> > .ClearFormatting
>> > .Text = ""
>> > End With
>> >
>> > .Execute Replace:=wdReplaceAll
>> > End With
>> > End With
>> >
>> > HTH,
>> > Dave
>> >
>> > "snsd" <snsd@discussions.microsoft.com> wrote in message
>> > news:33E58823-5018-4CAA-A2CF-D06C23CD11FE@microsoft.com...
>> > > Thanks to all of you for your excellent responses. They were all very
>> > > helpful
>> > > - and they work! I have one more question that I should have thought
>> > > of
>> > > beforehand. I'm assuming it's a simple adjustment to the code. I've
>> > > realized
>> > > that rather than having a list of INVALID characters, it would be
>> > > simpler
>> > > to
>> > > have a list of VALID characters. The valid characters are:
>> > >
>> > > ABCDEFGHIJKLMNOPQRSTUVWXYZ
>> > > abcdefghijklmnopqrstuvwxyz
>> > > 0123456789
>> > > /:-?()+.,'
>> > >
>> > > So, I would like to remove any characters that do NOT match the above
>> > > characters. I realized that when you get into all the non-keyboard
>> > > characters...that my list would be very difficult to manage. Any help
>> > > in
>> > > modifying the code to replace all characters NOT matching the above
>> > > would
>> > > be
>> > > great.
>> > >
>> > > Thanks,
>> > >
>> > > Dave
>> > >
>> > > "Graham Mayor" wrote:
>> > >
>> > >> 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.DefaultHighlightCol