Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 08-13-2012, 04:16 AM
krishnaoptif krishnaoptif is offline Windows XP Office 2007
Novice
 
Join Date: May 2012
Posts: 23
krishnaoptif is on a distinguished road
Arrow 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
Reply With Quote
  #2  
Old 08-13-2012, 07:09 AM
Venky62 Venky62 is offline Windows 7 64bit Office 2010 32bit
Advanced Beginner
 
Join Date: Jul 2012
Posts: 58
Venky62 is on a distinguished road
Default

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?
Reply With Quote
  #3  
Old 08-13-2012, 07:14 AM
krishnaoptif krishnaoptif is offline Windows XP Office 2007
Novice
 
Join Date: May 2012
Posts: 23
krishnaoptif is on a distinguished road
Default

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
Reply With Quote
  #4  
Old 08-13-2012, 07:32 AM
Venky62 Venky62 is offline Windows 7 64bit Office 2010 32bit
Advanced Beginner
 
Join Date: Jul 2012
Posts: 58
Venky62 is on a distinguished road
Default

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?
Reply With Quote
  #5  
Old 08-13-2012, 07:42 AM
krishnaoptif krishnaoptif is offline Windows XP Office 2007
Novice
 
Join Date: May 2012
Posts: 23
krishnaoptif is on a distinguished road
Default

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...
Reply With Quote
  #6  
Old 08-13-2012, 08:01 AM
Venky62 Venky62 is offline Windows 7 64bit Office 2010 32bit
Advanced Beginner
 
Join Date: Jul 2012
Posts: 58
Venky62 is on a distinguished road
Default

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
Reply With Quote
  #7  
Old 08-13-2012, 10:47 AM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,648
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Simpler:
Code:
Sub Demo()
With ActiveDocument
  MsgBox .Range(0, InStr(.Range.Text, "My String")).ComputeStatistics(wdStatisticWords) - 1
End With
End Sub
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #8  
Old 08-13-2012, 08:48 PM
Venky62 Venky62 is offline Windows 7 64bit Office 2010 32bit
Advanced Beginner
 
Join Date: Jul 2012
Posts: 58
Venky62 is on a distinguished road
Default

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.
Reply With Quote
  #9  
Old 08-13-2012, 08:58 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,648
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
[MS MVP - Word]
Reply With Quote
  #10  
Old 08-13-2012, 09:01 PM
Venky62 Venky62 is offline Windows 7 64bit Office 2010 32bit
Advanced Beginner
 
Join Date: Jul 2012
Posts: 58
Venky62 is on a distinguished road
Default

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.
Reply With Quote
Reply

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 0 09-07-2010 05:46 AM
Number formatting in Word table 8braitp Word Tables 0 08-27-2009 04:12 AM


All times are GMT -7. The time now is 03:39 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft