MS Office Forum / Word / Programming / October 2007
Counting Find & Replacements
|
|
Thread rating:  |
Conan Kelly - 27 Sep 2007 19:09 GMT 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
Jay Freedman - 27 Sep 2007 20:46 GMT See http://word.mvps.org/FAQs/MacrosVBA/GetNoOfReplacements.htm.
 Signature Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
> Hello all, > [quoted text clipped - 15 lines] > > Conan Kelly fumei - 28 Sep 2007 03:51 GMT 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
Greg Maxey - 28 Sep 2007 12:06 GMT I have an Add-In http://gregmaxey.mvps.org/Find_it_tool_bar.htm doing this sort of thing
 Signature Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
> It can be done much easier not using Selection. > [quoted text clipped - 40 lines] > Call ReplaceAndCount(",11,", "11") > End Sub fumei - 28 Sep 2007 04:21 GMT 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.
fumei - 28 Sep 2007 04:34 GMT 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.
Helmut Weber - 28 Sep 2007 09:50 GMT Hi,
you may find that discussion interesting as well:
http://www.adras.com/Number-of-Finds-and-Replaces.t5566-105-3.html
 Signature Greetings from Bavaria, Germany Helmut Weber, MVP WordVBA "red.sys" & chr(64) & "t-online.de" Word 2002, Windows 2000 (german versions)
Russ - 28 Sep 2007 10:04 GMT Off Track? But who gets to the finish line first? ;-)
> 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.
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
Jay Freedman - 28 Sep 2007 14:23 GMT 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 FAQ: http://word.mvps.org Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
>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 [quoted text clipped - 39 lines] > >Done. fumei - 01 Oct 2007 18:18 GMT 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......
|
|
|