Re: Searching for and Replacing Multiple items in a macro

Tech-Archive recommends: Fix windows errors by optimizing your registry



As I don't have editorial control over the wildcard article I wrote (with
Klaus) for the MVP web site, I have been making the revisions on my own web
site - see instead http://www.gmayor.com/replace_using_wildcards.htm which
clarifies the issue.

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

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

Dave Lett wrote:
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@xxxxxxxx> wrote in message
news:1154707539.587933.62260@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:33E58823-5018-4CAA-A2CF-D06C23CD11FE@xxxxxxxxxxxxxxxx
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


.



Relevant Pages

  • Re: How do I insert a phi character in word 2007 using keyboard short
    ... CharMap displays the characters at a much smaller size than Insert ... And its display is likely to be covered by a document ... Word MVP web site http://word.mvps.org ...
    (microsoft.public.word.docmanagement)
  • Re: Accent with Ukrainian keyboard layout
    ... If the font you are using does not contain the characters you want then the ... Word MVP web site http://word.mvps.org ... Slovenian characters with accents are not useful in Ukraine. ...
    (microsoft.public.word.docmanagement)
  • Re: Template Text Box
    ... characters that will nearly fill the space. ... Graham Mayor - Word MVP ... Word MVP web site http://word.mvps.org ...
    (microsoft.public.word.docmanagement)
  • Re: Template Text Box
    ... Graham Mayor - Word MVP ... Word MVP web site http://word.mvps.org ... to figure how many characters that would contain. ...
    (microsoft.public.word.docmanagement)
  • Re: Embedding webfont: a real-life example
    ... >> first platform independent kannada web site built with font embedding ... > displayed in such a way they look like Kannada characters. ... > A similar problem exists with the Arabic script, ...
    (comp.infosystems.www.authoring.stylesheets)