Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Search & Replace -- Multiple Occurances

0 views
Skip to first unread message

Steve

unread,
Feb 7, 2003, 6:56:13 PM2/7/03
to
Many thanks to Jonathon West, Word MVP for helping on Jan. 23 with multiple
search & replace.

I need to generalize his search & replace routine to cover any arbitrary
number of characters in documents I get from contributors. For example, I
sometimes get a doc. with "( text )" -- note the spaces after and before
parens. Sometimes there is one space, sometimes two, etc. I also get docs
with spaces before commas or periods, e.g., "some text , next clause"). I
adapted & wrote following macro to handle arbitrary numbers of characters
(Thanks to J. West!):

QUESTIONS:
1. Is this the best approach to handling arbitrary numbers of characters
such as I described?
2. I also need to convert multiple consecutive paragraph returns to a single
return. This approach in this macro works unless I have two consecutive
returns at the very bottom of the doc in which case wdReplaceAll goes into
an infinite loop. How can I solve this special problem?
3. Jonathon--why the different array size initializations for vFindText and
vReplText?

Sub CleanUpDoc()
' This macro removes extra spaces, spaces before commas, spaces after
' open paren and before close paren, spaces before paragraph returns,
' spaces after paragraph returns and multiple paragraph returns.
' Adapted 2/7/03 from Jonathon West, Word MVP, http://www.multilinker.com

Dim vFindText As Variant
Dim vReplText As Variant
Dim i As Long

vFindText = Array(Chr(180), "( ", " )", " ,", " .", " ")
vReplText = Array(ChrW(660), "(", ")", ",", ".", " ")

With ActiveDocument.Content.Find
.ClearFormatting
For i = 1 To UBound(vFindText)
Do While .Execute(FindText:=vFindText(i), _
Forward:=True, _
Format:=True) = True
.Execute FindText:=vFindText(i), _
Forward:=True, _
Format:=True, _
Replacewith:=vReplText(i), _
Replace:=wdReplaceAll
Loop
Next i
End With
End Sub


Klaus Linke

unread,
Feb 8, 2003, 4:39:14 AM2/8/03
to
Hi Steve,

> 1. Is this the best approach to handling arbitrary numbers
> of characters such as I described?

Instead of replacing "( " with "(", you might replace "(^w)" with "(".
This would remove an arbitrary number of spaces (and non-breaking spaces
and tabs) in one go.

In case you want to replace multiple spaces/tabs/non-breaking spaces with
one space, you could replace " ^w" with " ".


I think you've got an answer to (2.) in another thread.


> 3. Jonathon--why the different array size initializations for
> vFindText and vReplText?

If you refer to Chr(180) and ChrW(660), that isn't an array size
initialization.

Those are the first elements in the arrays, and they replace Chr(180) = ´
with the character ChrW(660) which is a phonetic character that looks like
a question mark without the dot ("glottal stop").

I'm sure this is a typo.

Probably, since many users type ´ instead of an apostrophe, it was meant to
replace ChrW(180) with ChrW(8217) or ChrW(&H2019) (since the apostrophe has
Unicode U+2019).


Greetings,
Klaus

Steve

unread,
Feb 8, 2003, 9:55:30 PM2/8/03
to
Hi, Klaus...many thanks for helping. Using debug.print, I noticed that the
first elements in the array were not the ones I had defined (they were the
ones you mentioned). I was curious about that. I "cheated" in my solution by
starting at element 1 rather than element 0, thus ignoring those first two
array elements.

I hadn't thought of using the white-space character (^w) -- excellent
suggestion.

I usually want to replace multiple consecutive tabs with a single tab, not a
space, so I'll probably stick with my approach.

Guten abend,
Steve


"Klaus Linke" <fotosatz...@t-online.de> wrote in message
news:uyoPsX1zCHA.2288@TK2MSFTNGP09...

0 new messages