#1
|
|||
|
|||
how to know word number by VBA
Hi Experts,
Please find below my code.... it works for me but i want the words Count befor this replaced content.. Code:
With ThisDocument.Content.Find .Text = Search2 .Replacement.Text = Replace1 .Replacement.Highlight = True .Wrap = wdFindContinue .Execute Replace:=wdReplaceOne End With Last edited by krishnaoptif; 08-13-2012 at 06:47 AM. Reason: need some changes |
#2
|
|||
|
|||
Your question is not clear. Do you want to know the total words in your document, or the no. of words you have replaced through the Find and Replace procedure?
|
#3
|
|||
|
|||
not actually i want know number of words before replaced contants...
as i have below word file data in a page and want to replace "AN CC" to "Ankit Nirbal Chaudhay Chuck" so i need count of word before "AN CC" contant so ans would be 4 (count of kk pp ss ffff) kk pp ss ffff AN CC PP SS |
#4
|
|||
|
|||
Still not clear enough to answer. You want the count of all the words before the text you plan to replace - in this case AN CC. The next thing one would want to know is how much of the text: beginning of line, beginning of sentence, beginning of paragraph or the whole document?
And also will there be more than one hits for the Find? |
#5
|
|||
|
|||
i need count of words from document start to before "AN CC" containt...
Let say... i have 10 or 15 or more pages before "AN CC" then i need count of all above page's words count till "AN CC" Containt.. not after this... |
#6
|
|||
|
|||
Okay here is the code. I am assuming you know how to enter the code.
I have used "AN CC" as the search string - from the example you gave. A message box gives you the total number of words before this string. You can modify this code to do with it whatever you want. Code:
Sub countwords() Dim intcount As Integer Selection.HomeKey Unit:=wdStory, Extend:=wdMove With Selection.Find .Text = "AN CC" .Forward = True .Wrap = wdFindStop .Execute End With Selection.HomeKey Unit:=wdStory, Extend:=wdExtend intcount = Selection.Words.Count Call MsgBox("The total number of words before AN CC are " & intcount, vbExclamation, Application.Name) End Sub |
#7
|
||||
|
||||
Simpler:
Code:
Sub Demo() With ActiveDocument MsgBox .Range(0, InStr(.Range.Text, "My String")).ComputeStatistics(wdStatisticWords) - 1 End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
|||
|
|||
Great macro, Paul, as always.
While testing the code I wrote and the one you posted, I found that my code gives wrong results. If the number of words before the search string is 4, then my code gives 5. If the number is 25, it gives 27. And the margin of error increases, with increasing number of words before the search string.Your code, on the other hand, is perfect. What is it that I am doing wrong? Thanks in advance. |
#9
|
||||
|
||||
For starters, when you use code like 'Selection.Words.Count', spaces and punctuation characters are both treated as word separators. So, even on a single number with thousand and decimal separators, you'll get a multi-word count of 3 or more.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#10
|
|||
|
|||
Thanks, Paul. That explains it. I was guessing as much, but wanted to be sure. So the only correct way is to use computerStatistics function. Got it. Thanks again.
|
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
generating a number in Word | John P | Word | 1 | 05-31-2012 10:29 PM |
auto number in word | expert4knowledge | Word | 7 | 05-31-2012 02:37 PM |
Mailing: how to make the "page number" in Word is the same as "row number" in excel w | Jamal NUMAN | Word | 1 | 09-03-2011 11:37 AM |
Textbox Number changes when I open Word | t0m46 | Word VBA | 0 | 09-07-2010 05:46 AM |
Number formatting in Word table | 8braitp | Word Tables | 0 | 08-27-2009 04:12 AM |