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

replace all specials characters with specific string

0 views
Skip to first unread message

Jean-Michel decoster

unread,
Nov 6, 1998, 3:00:00 AM11/6/98
to
I need to replace in a document all specials characters like Symbol or
other charset.
but characters have been place in document with instruction :
insertsymbol.

i try with .find ^uNNNNN
where NNNNN is the unicode
but if i must replace all the unicode combinaison it'il take a long time

Is somebody know a solution to create a sub to do something like

sub replaceSpecialChar ()
Activedocument.find specialcharacter
.replace "<UNI>"+unicode+"</UNI>"
end sub

thanks a lot.

Bill Coan

unread,
Nov 6, 1998, 3:00:00 AM11/6/98
to
Hello Jean-Michel,

You can accomplish your goal easily using Word's find-and-replace command.
Also, if desired, you can record your actions to create a macro.

It will take two passes for each character. Here's how to proceed:

1. Choose Replace from the Edit menu.
2. Click MORE... to reveal the full dialog box.
3. Check Use Wildcards.
4. In the Find What box, enter (^NNNN) where NNNN is the unicode number. Be
sure to include the Parentheses ().
5. In the Replace With box enter <UNI>\1</UNI>
6. Choose replace all.

This will surround the specified character with tags, like so:

BEFORE: A
AFTER: <UNI>A</UNI>

7. Uncheck Use Wildcards.
8. In the Find What box, enter ^NNNN
9. In the Replace With box enter NNNN
10. Choose replace all.

BEFORE: <UNI>A</UNI>
AFTER: <UNI>0065</UNI>

If you decide to record these actions, you can place the resulting code
inside a loop and perform both passes on as many character values as
desired.
--
Bill Coan
Neenah, Wisconsin


Jean-Michel decoster wrote in message
<36438225...@club-internet.fr>...

Greg Safford

unread,
Nov 7, 1998, 3:00:00 AM11/7/98
to
I think that your want to iterate through the pertinent characters
collections. There are many characters in a doc but fewer than unicode char
numbers in many instances.

Like maybe:

Dim rngChar As Range
For each rngChar in ActiveDocument.Content.Characters
if AscW(rngChar.text) <> Asc(rngChar.Text) then
rngChar.Text = "<uni>" & AscW(rngChar.Text) & "<\uni>"
end if
next rngChar


Just a thought.

Bill Coan

unread,
Nov 7, 1998, 3:00:00 AM11/7/98
to
Hi Greg,

Iterating through Word's character collection is a perfectly logical (in
fact, the most logical) method for examining all characters in a document.
Unfortunately, it can be many, many times slower than using Word's
find-and-replace command.

I recently converted a routine from one method to the other and cut
execution time by 80%. The reason, I believe, is that the find-and-replace
command relies on the underlying C code rather than on vba. Also, Word's
wildcard functions can search for entire ranges of character values at a
time, if desired, which can reduce the number of passes required.

--
Bill Coan
Neenah, Wisconsin


Greg Safford wrote in message <7220so$m38$1...@camel0.mindspring.com>...

Greg Safford

unread,
Nov 7, 1998, 3:00:00 AM11/7/98
to
You are right, of course. I neglected to consider that ranges of character
numbers could be found. It isn't a priori obvious that one method should be
faster than the other, since one presumes that when searching for any
character from say, 256 to 64,000, the search routine either checks each
character or runs a series of searches. However, your suggestion regarding C
code sounds plausible, and, in any case, I am confident that I am doing much
wrong in implementing search and replace operations, as, to my
disappointment, most of my basic processing routines seem to require much
more time to run than did the Wordbasic routines they replaced.

Bill Coan wrote in message ...

Bill Coan

unread,
Nov 7, 1998, 3:00:00 AM11/7/98
to
Hi Greg,

I've found many vba routines to be slower than equivalent wordbasic
routines, too. This is especially true of routines that make use of
collections that happen to have dozens and dozens if not hundreds and
hundreds or thousands and thousands of members. In fact, some collections,
like the ShapeRange collection, can be extremely slow even if you keep them
small by basing them on a limited range of a document.

If you haven't explored Word's wildcards for find-and-replace, you may want
to look into them. I've seen some truly ingenious applications of them in
this and other Word newsgroups. There's a fairly good topic that addresses
them in the Help index, listed under "Examples of Search Wildcards." My own
interest in wildcards was sparked by a posting by Wolfgang Hutter, dated
almost two years ago. Until just a few days ago, I kept a printout of his
posting taped to the side of my monitor for easy reference. I haven't seen a
posting from him for quite a while now. If you happen to read this, Wolfie,
thanks again!

My own explorations of ways to implement the Find() method have been
limited, to date, to analyzing recordings of searches and then tweaking the
settings or building a loop around the Find statement. I've been pleased at
the boost in speed relative to routines based on collections.


--
Bill Coan
Neenah, Wisconsin


Greg Safford wrote in message <722bc0$61i$1...@camel29.mindspring.com>...

Greg Safford

unread,
Nov 7, 1998, 3:00:00 AM11/7/98
to
>If you haven't explored Word's wildcards for find-and-replace, you may want
>to look into them. I've seen some truly ingenious applications of them in
>this and other Word newsgroups. There's a fairly good topic that addresses
>them in the Help index, listed under "Examples of Search Wildcards." My own
>interest in wildcards was sparked by a posting by Wolfgang Hutter, dated
>almost two years ago. Until just a few days ago, I kept a printout of his
>posting taped to the side of my monitor for easy reference. I haven't seen
a
>posting from him for quite a while now. If you happen to read this, Wolfie,
>thanks again!

I am embarrassed to admit, that I use wildcards all the time. The help isn't
too easy to find now (in Word 6 the PatternMatch help topic was available
from the Find dialog. I just didn't notice its application to the original
problem--not just ignorant, also stupid :)

Wildcards searches involve oddities, for example, I keep the following bit
of code around to remind me of a problem that broke a much bigger routine
some time back:

Sub RangeReplaceSkipDemo()
Dim rngX As Range
Set rngX = ActiveDocument.Content.Duplicate
With rngX.Find
.Text = "Now"
.Replacement.Text = ""
' .MatchWildcards = True
Rem Unless .MatchWildcards is set true, the second Execute
Rem finds a new range.
Rem Doesn't work this way with a find on the Selection object.
Do While .Execute(Replace:=wdReplaceNone)
rngX.Select
.Execute Replace:=wdReplaceOne
rngX.Select
Selection.Collapse wdCollapseEnd
Loop
End With
End Sub

Haven't tested this with SR2 yet.

Bill Coan

unread,
Nov 7, 1998, 3:00:00 AM11/7/98
to
Hi Greg,

Thanks for pointing out this interesting anomaly. Here's another one: If
Word finds the beginning of a pattern match at the end of a document, it
reports a full hit. Not only that, but if you interrupt a macro upon such a
hit, and check the start and end positions of the selection, you find the
end position is zero. Strangely, no selection is visible on screen. Yet, if
you hit the right arrow key at this point, the cursor starts flashing
between the first and second characters of the document. If you hit the left
arrow key, the cursor flashes immediately before the start of the matched
text. I haven't checked to see if the behavior changes when Use Wildcards is
unchecked.

Thanks again for the info.


--
Bill Coan
Neenah, Wisconsin


Greg Safford wrote in message <722rgi$bu5$1...@camel25.mindspring.com>...

David Foster

unread,
Nov 9, 1998, 3:00:00 AM11/9/98
to
On Fri, 06 Nov 1998 23:11:38 +0000, Jean-Michel decoster
<deco...@club-internet.fr> wrote:

>I need to replace in a document all specials characters like Symbol or
>other charset.
>but characters have been place in document with instruction :
>insertsymbol.
>
>i try with .find ^uNNNNN
>where NNNNN is the unicode
>but if i must replace all the unicode combinaison it'il take a long time

Heh, that it will. Over 65000 search-replace loops.

The first thing you'll need to do is decide exactly what is a "Special
Character". Does font matter? Are all WingDing characters special?
How about the 128-255 ascii range? Those are special characters here
in the US, but French docs probably have a lot of them.

>Is somebody know a solution to create a sub to do something like
>
>sub replaceSpecialChar ()
> Activedocument.find specialcharacter
> .replace "<UNI>"+unicode+"</UNI>"
>end sub

You'll need to use a range for this, and loop through the document
looking for characters in the range. This example assumes anything
unicode-sized is to be replaced.

Dim rng As Range
Dim nUnicode As Long
Set rng = ActiveDocument.Range

With rng.Find
.ClearFormatting
.Text = "[" & ChrW(256) & "-" & ChrW(65535) & "]"
.MatchWildcards = True
While .Execute
nUnicode = AscW(rng.Text)
rng.Text = "<UNI>" & CStr(nUnicode) & "</UNI>"
rng.Collapse wdCollapseEnd
Wend
End With


--
David Foster | "But I've been to the pointless forest,
dfo...@panix.com | and it isn't pointless at all."
finger for PGP key | -- Oblio, The Point

0 new messages