Counting word and adding the result of a search and replace

J

John Doue

Hi,

I need to write a word which counts the word of the active document and
adds to the result the number of replacements of a given character by
another character. Any help will be greatly appreciated.

Thanks
 
H

Helmut Weber

Hi John,

count ocurrences of the string "lazy":

Sub test6789a()
Dim lCnt As Long
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "lazy"
' if you like, add:
' .MatchWholeWord = True
' .MatchCase = True
While .Execute
lCnt = lCnt + 1
Wend
End With
MsgBox lCnt
End Sub

Count number of replacements:

Sub test6789b()
Dim lCnt As Long
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "t"
.Replacement.Text = "m"
While .Execute
lCnt = lCnt + 1
Wend
End With
MsgBox lCnt
End Sub

Of course, there are other ways.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

John Doue

Helmut said:
Hi John,

count ocurrences of the string "lazy":

Sub test6789a()
Dim lCnt As Long
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "lazy"
' if you like, add:
' .MatchWholeWord = True
' .MatchCase = True
While .Execute
lCnt = lCnt + 1
Wend
End With
MsgBox lCnt
End Sub

Count number of replacements:

Sub test6789b()
Dim lCnt As Long
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "t"
.Replacement.Text = "m"
While .Execute
lCnt = lCnt + 1
Wend
End With
MsgBox lCnt
End Sub

Of course, there are other ways.
Thanks Helmut, but you missed the point that I need to add the result to
the number of words of the document, which is where I am stuck. Can you
go a little further, please?
 
G

Greg Maxey

Considering how the initial question was worded it is little wonder
that Helmut missed the point.

"I need to write a word that counts the word ..."

What is that supposed to mean?

Why don't you:

a. Explain clearly what it is that you are trying to do.
b. Show us what you have done so far in your attempt to do a. above

That might help us provide a better answer.
 
G

Greg Maxey

Helmut,

You replace doesnt' replace anything ;-)

Sub test6789bModified()
Dim lCnt As Long
Dim wCnt As Long
Dim rDcm As Range
wCnt = ActiveDocument.ComputeStatistics(wdStatisticWords)
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "t"
.Replacement.Text = "m"
While .Execute
lCnt = lCnt + 1
Wend
rDcm.Start = ActiveDocument.Range.Start
.Execute Replace:=wdReplaceAll
End With
MsgBox wCnt + lCnt
End Sub
 
J

John Doue

Greg said:
Considering how the initial question was worded it is little wonder
that Helmut missed the point.

"I need to write a word that counts the word ..."

What is that supposed to mean?

Why don't you:

a. Explain clearly what it is that you are trying to do.
b. Show us what you have done so far in your attempt to do a. above

That might help us provide a better answer.
Indeed, I am sorry my haste made me make to mistakes in wording my query
which should have read "macro which counts the number of words". I
apologize for being hasty.

This being said, it seems to me you might be more intent on setting the
record straight than Helmut who did his best to help, even if my post
left much to be desired.

Regards
 
G

Greg Maxey

John,

Again, what is this suppose to mean?

"This being said, it seems to me you might be more intent on setting
the record straight than Helmut who did his best to help, even if my
post left much to be desired."

What record?

After I replied to your last post, I read Helmut's response and
attempted to modify the code that he provided to a: acutally make a
replacement, and b. add the number of replacements to the number of
words in the document.

You still haven't clearly defined your problem and so my attempt is
just a guess at what you actually want.

Is the record straight ;-)
 
J

John Doue

Greg said:
John,

Again, what is this suppose to mean?

"This being said, it seems to me you might be more intent on setting
the record straight than Helmut who did his best to help, even if my
post left much to be desired."

What record?

After I replied to your last post, I read Helmut's response and
attempted to modify the code that he provided to a: acutally make a
replacement, and b. add the number of replacements to the number of
words in the document.

You still haven't clearly defined your problem and so my attempt is
just a guess at what you actually want.

Is the record straight ;-)
Yes, Greg, it is, your macro does exactly what I want, thanks. Just need
to figure out how to insert text in this message box that does not seem
to accept my usual format like in:
MsgBox "Revision Marks rétablies ; Nombre de doubles espaces supprimés
:" & Str(WordNum)

Yes, the record is straight and I appreciate your help.

Regards
 
G

Greg Maxey

Do you mean something like this:

MsgBox "There are " & wCnt & " words in the document " _
& lCnt & " replacements. For a total count of " _
& lCnt + wCnt
 
J

John Doue

Greg said:
Do you mean something like this:

MsgBox "There are " & wCnt & " words in the document " _
& lCnt & " replacements. For a total count of " _
& lCnt + wCnt
Much obliged, yes, you did get ... my point ... :).
 
H

Helmut Weber

Hi Greg,
Your replace doesnt' replace anything ;-)

da***d, indeed.

With a little help from my friends,
I know now,
I must have missed some important detail. :)

Have a nice day.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

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