Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2006

Tip: Looking for answers? Try searching our database.

Counting word and adding the result of a search and replace

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John Doue - 13 Oct 2006 09:13 GMT
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
Signature

John Doue

Helmut Weber - 13 Oct 2006 11:14 GMT
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.

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

John Doue - 13 Oct 2006 15:04 GMT
> Hi John,
>
[quoted text clipped - 33 lines]
>
> 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?

Signature

John Doue

Greg Maxey - 13 Oct 2006 15:12 GMT
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.

> > Hi John,
> >
[quoted text clipped - 37 lines]
> the number of words of the document, which is where I am stuck. Can you
> go a little further, please?
John Doue - 13 Oct 2006 15:47 GMT
> Considering how the initial question was worded it is little wonder
> that Helmut missed the point.
[quoted text clipped - 51 lines]
>>the number of words of the document, which is where I am stuck. Can you
>>go a little further, please?

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

Signature

John Doue

Greg Maxey - 13 Oct 2006 15:56 GMT
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 ;-)

> > Considering how the initial question was worded it is little wonder
> > that Helmut missed the point.
[quoted text clipped - 64 lines]
>
> Regards
John Doue - 13 Oct 2006 16:51 GMT
> John,
>
[quoted text clipped - 84 lines]
>>
>> Regards

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
Signature

John Doue

Greg Maxey - 13 Oct 2006 17:14 GMT
Do you mean something like this:

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

> > John,
> >
[quoted text clipped - 97 lines]
>
> Regards
John Doue - 13 Oct 2006 18:24 GMT
> Do you mean something like this:
>
[quoted text clipped - 103 lines]
>>
>>Regards

Much obliged, yes, you did get ... my point ... :).

Signature

John Doue

Greg Maxey - 13 Oct 2006 15:24 GMT
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

> Hi John,
>
[quoted text clipped - 41 lines]
> Win XP, Office 2003
> "red.sys" & Chr$(64) & "t-online.de"
Helmut Weber - 13 Oct 2006 21:01 GMT
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.

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.