MS Office Forum / Word / Programming / August 2006
Searching for and Replacing Multiple items in a macro
|
|
Thread rating:  |
snsd - 03 Aug 2006 12:23 GMT 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
Greg Maxey - 03 Aug 2006 12:58 GMT 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
> Hi: > [quoted text clipped - 39 lines] > Selection.Find.Execute Replace:=wdReplaceAll > End Sub Graham Mayor - 03 Aug 2006 13:33 GMT 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
 Signature <>>< ><<> ><<> <>>< ><<> <>>< <>><<> Graham Mayor - Word MVP
My web site www.gmayor.com Word MVP web site http://word.mvps.org <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> Hi: > [quoted text clipped - 41 lines] > Selection.Find.Execute Replace:=wdReplaceAll > End Sub snsd - 04 Aug 2006 15:46 GMT 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
> The following will replace a list of characters each in quotes and separated > by commas as below with a green highlighted space: [quoted text clipped - 73 lines] > > Selection.Find.Execute Replace:=wdReplaceAll > > End Sub Dave Lett - 04 Aug 2006 15:58 GMT 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
> Thanks to all of you for your excellent responses. They were all very > helpful [quoted text clipped - 99 lines] >> > Selection.Find.Execute Replace:=wdReplaceAll >> > End Sub snsd - 04 Aug 2006 16:25 GMT 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
> Hi, > [quoted text clipped - 121 lines] > >> > Selection.Find.Execute Replace:=wdReplaceAll > >> > End Sub Greg Maxey - 04 Aug 2006 17:05 GMT 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.
> 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.) [quoted text clipped - 159 lines] > > >> > Selection.Find.Execute Replace:=wdReplaceAll > > >> > End Sub snsd - 04 Aug 2006 17:34 GMT 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
> I didn't test the code, but part of the problem is explained by using > !A-z. [quoted text clipped - 169 lines] > > > >> > Selection.Find.Execute Replace:=wdReplaceAll > > > >> > End Sub Greg Maxey - 04 Aug 2006 18:12 GMT Hmm,
Works for me. Try this: 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
The ^13 is to keep from adding an marking a space next to the paragraph mark.
You will have to use "straight" quotes for ' to work. I forget the find and replace routine to replace "smart" curly quotes with straight quotes.
> 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 [quoted text clipped - 179 lines] > > > > >> > Selection.Find.Execute Replace:=wdReplaceAll > > > > >> > End Sub Dave Lett - 04 Aug 2006 17:38 GMT 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
>I didn't test the code, but part of the problem is explained by using > !A-z. [quoted text clipped - 182 lines] >> > >> > Selection.Find.Execute Replace:=wdReplaceAll >> > >> > End Sub snsd - 04 Aug 2006 17:54 GMT Greg/Dave:
One other thing I forgot to ask. In Dave's code, instead of REMOVING the invalid characters by using .Text = "", I have decided to REPLACE the invalid characters with the letter X. I would like to highlight the letter X. I have added Options.DefaultHighlightColorIndex = wdRed. I have seen that others have .Replacement.Highlight = True in their code. How can I add the highlighting functionality to Dave's code?
Thanks,
Dave
> Hi Greg, > [quoted text clipped - 193 lines] > >> > >> > Selection.Find.Execute Replace:=wdReplaceAll > >> > >> > End Sub Greg Maxey - 04 Aug 2006 18:03 GMT Dave,
I agree. Graham Mayor has pointed this out in his article:
http://www.gmayor.com/replace_using_wildcards.htm
I don't know why the FAQ hasn't been corrected. Well corrected isn't really the right term as the statement is true. The problem is that [0-9A-z] firnds more than just any number or letter.
A-Z is ASCII 65-90 a-z is ASCII 97-122
When you join A-z you get 65-122
But: 91 = [, 92 = \, 93 = ], 94= ^, 95 = _, and 96 =`
See: Sub Test() Dim i As Long For i = 65 To 122 Debug.Print Chr(i) Next End Sub
> Hi Greg, > [quoted text clipped - 193 lines] > >> > >> > Selection.Find.Execute Replace:=wdReplaceAll > >> > >> > End Sub Graham Mayor - 05 Aug 2006 07:45 GMT 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.
 Signature <>>< ><<> ><<> <>>< ><<> <>>< <>><<> Graham Mayor - Word MVP
My web site www.gmayor.com Word MVP web site http://word.mvps.org <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> Hi Greg, > [quoted text clipped - 191 lines] >>>>>>> Selection.Find.Execute Replace:=wdReplaceAll >>>>>>> End Sub Greg Maxey - 04 Aug 2006 16:08 GMT 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
> 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 [quoted text clipped - 102 lines] > > > Selection.Find.Execute Replace:=wdReplaceAll > > > End Sub Helmut Weber - 03 Aug 2006 15:48 GMT 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
 Signature Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003 "red.sys" & Chr$(64) & "t-online.de"
|
|
|