Counting Find & Replacements

C

Conan Kelly

Hello all,

When I do a normal Find & Replace>Replace All in Word, a message box pops up
at the end telling me how many replacements have been made.

But when I execute the following line:

Selection.Find.Execute Replace:=wdReplaceAll, findtext:=",11,",
replacewith:="11,"

I should get back a true or false, correct?

Is there a property on the Find object or the Replacement object that keeps
track of how many replacements have been made? If not, how would I keep
track of the number of replacements that have been made?

Thanks for any help anyone can provide,

Conan Kelly
 
F

fumei via OfficeKB.com

It can be done much easier not using Selection.

Sub ReplaceAndCount()
Dim r As Range
Dim counter As Long
Set r = ActiveDocument.Range
With r.Find
.ClearFormatting
Do While .Execute(Findtext:=",11,", Forward:=True) = True
With r
.Text = "11"
.Collapse Direction:=wdCollapseEnd
End With
counter = counter + 1
Loop
End With
Set r = Nothing
MsgBox counter & " replacements made."
End Sub

And of course it could be used to take any two input strings as parameters.

Sub ReplaceAndCount(strOld As String, strNew As String)
Dim r As Range
Dim counter As Long
Set r = ActiveDocument.Range
With r.Find
.ClearFormatting
Do While .Execute(Findtext:=strOld, Forward:=True) = True
With r
.Text = strNew
.Collapse Direction:=wdCollapseEnd
End With
counter = counter + 1
Loop
End With
Set r = Nothing
MsgBox counter & " replacements made."
End Sub


Sub GiveStrings()
Call ReplaceAndCount(",11,", "11")
End Sub
 
F

fumei via OfficeKB.com

With all due respect to the Word MVP org bunch, counting ALL the characters
in a document to get a replacement counter seems utter madness to me. And
completely not needed.

Further, even the word count function seems way over the top. It needs to
turns screen updating off (because it uses Selection). It counts all the
characters....TWICE! It makes REAL replacement of text....then has to do an
Undo to put the text back. Then put screen updating back on. All for a word
count???? What on earth does doing a text replacement have to do with
counting words?

Counting all the characters? Ummm, in a 500 page document...gee, that sounds
very efficient.

Function MyWordCount(strIn As String) As Long
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
.ClearFormatting
Do While .Execute(Findtext:=strIn, Forward:=True) = True
MyWordCount = MyWordCount + 1
Loop
End With
End Function


Sub GetWordCount()
Dim strIn As String
strIn = InputBox("Word, or phrase?", "My text counter")
MsgBox "There are " & MyWordCount(strIn) & _
" instance of the word, or phrase: " & _
strIn
End Sub

No screen updating changes required, as there is no use of Selection. No
replacement of text required. No Undo of those replacements required. No
calculations required...simply....a counter.

Found one.....counter + 1
Found one.....counter + 1

Display the count.

Done.
 
F

fumei via OfficeKB.com

Sorry, I do not want to appear hypercritical of the Word MVP Org bunch.
There a number of fabulous and useful articles there, and I certainly direct
and recommend people to them.

But that particular article - mostly because it uses Selection...and that
crazy character counting - IMHO, is way off track.
 
J

Jay Freedman

Hi fumei,

You may be right, but before we all agree with you, let me point out a
few things:

- The article at
http://www.word.mvps.org/FAQs/MacrosVBA/GetNoOfReplacements.htm is an
old one, and we've all learned a lot more about VBA since then. You're
right that it should be rewritten using a Range instead of the
Selection. Unfortunately, Dave Rado is no longer available to do the
rewrite, and the rest of us are busy here. :)

- The method you showed, using a counter, is also shown at
http://www.word.mvps.org/FAQs/MacrosVBA/NoTimesTextInDoc.htm. However,
it's written for a Find rather than a Replace.

- Dave's macro does _not_ count all the characters in a document, at
least not in the sense of taking time to go "1, 2, 3, ...". The
Document object automatically maintains a .Characters.Count property
at all times, and the macro merely retrieves the current value. It's
very fast, and not at all "utter madness".

- The statement at the bottom of the article is probably true, that
ReplaceAll is much faster than one-at-a-time Replace with a counter.
I'd be interested to see a benchmark that tried comparing the two
methods over a large range of document sizes, but I don't have time to
do it myself.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
F

fumei via OfficeKB.com

Hi Jay. That is why I posted a very careful statement that I had no
intention of being hypercritical. I understand fully that the article is old
- I have seen it for a long time. I was not suggesting anything really.

Yes, I know that character count is there. My point was that there is a
calculation, an operation that is, IMO, not needed. That count still has to
be retreived, and stored, retrieved again and stored, and a calculation done
between the stored values.

However, my strongest objection is the conceptual use of a Replace operation.
To write text, then remove the inserted text. It just seems, conceptually, a
lot of I/O that simply is not needed. I fail to see the need for any replace
operation at all.

I am not looking for anyone to agree with me, or not. My comments were just
that...comments. Although...yes, I will admit I used some perhaps
inappropriate words, like "utter madness". My apologies.

Chuckle...yes, I wish I had time to do some serious time/speed comparisons as
well. I have a monster 1,100 page technical specs document that would be fun
to test on......
 

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