Weird problem Word 2000

M

Marc Hillman

I have a relatively straightforward piece of VBA that
works fine in Word 2002, Word 2003 but not in Word 2000.
Anyone hazard a guess why ?

With ActiveDocument.Content.Find
.Text = "blah"
.Replacement.Text = "blah blah"
.Replacement.Font.Color = whatever
.Forward = True
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
if .Found = True then something ...
End With

Now the problem is that in all 3 environments, the text
replacement is occuring, however ".Found" does not return
True in the 2000 environment.
Why not, and is there a workaround?
 
J

Jay Freedman

Marc said:
I have a relatively straightforward piece of VBA that
works fine in Word 2002, Word 2003 but not in Word 2000.
Anyone hazard a guess why ?

With ActiveDocument.Content.Find
.Text = "blah"
.Replacement.Text = "blah blah"
.Replacement.Font.Color = whatever
.Forward = True
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
if .Found = True then something ...
End With

Now the problem is that in all 3 environments, the text
replacement is occuring, however ".Found" does not return
True in the 2000 environment.
Why not, and is there a workaround?

I have no idea why there's a difference among versions, but try this
instead: The .Execute method can be called as a function that returns a
Boolean value, and that value should be the same as .Found. Use the syntax

If .Execute(Replace:=wdReplaceAll) Then
' ... something
End If

The parentheses are required for a function call (just a VB/VBA oddity).

This *may* work better than separately testing .Found. Let us know...
 
D

David Sisson

Yep, I have W2000 and it fails the same way.

I did a search on Find.found and found a message from Jonathan West
describing the problem.

Here's a clip from that message.
I'm failing to get this statement
Do While .Execute(Replace:=wdReplaceAll­)
loop

to run until there is nothing left to replace. (More complete example
below,
but this is the crux of the matter.)

The reason this works the way it does that Replace:=wdReplaceAll means
that
the find/replace process continues until it can't find anything. At
that
point, the Execute method ends, and because it ended with not being
able to
find anything, it returns the value False.

..Execute(Replace:=wdReplaceAll­)

is functuionally equivalent in all respects (except perhaps
performance) to
this

Do While .Execute(Replace:=wdReplaceOne­)
Loop


Now look at that loop, and ask yourself what will be the value returned
by
Execute at the point it drops out of the loop. Yup, it's False!
I've tried using .Found for the same purpose. It always evaluates as false,
even though the macro makes the replacement in the document.

That is because the Found property returns the value returned by the
last
time the Execute macro ran

OK, there is an alternative. What you are doing here is replacing a
string
with a shorter string. This means that the number of Characters in the
document will change if the Find found anything, and you can use this
difference in the length of the document to test the exit condition for
the
loop, like this

Sub Despacify()
Dim i as Long
With ActiveDocument.Content.Find
.Wrap = wdFindContinue
.Forward = True
.ClearFormatting
.Replacement.ClearFormatting
.Format = False


'read away extra spaces
.Text = " ^p"
.Replacement.Text = "^p"
Do
i = ActiveDocument.Content.Charact­ers.Count
.Execute Replace:=wdReplaceAll
Loop Until i = ActiveDocument.Content.Charact­ers.Count
End With
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top