MartyConnelly
martyconnelly at shaw.ca
Sat Mar 12 18:17:31 CST 2005
Don't know if you want from Word or a text file I suppose you could copy and paste a single page then run this macro Sub FullWordcount() 'intrinsic Word Count function does not count words in text boxes ' this also counts words in text boxes Dim Wordcount As Long Wordcount = ActiveDocument.BuiltInDocumentProperties(wdPropertyWords) For Each ashape In ActiveDocument.Shapes If ashape.TextFrame.HasText Then Wordcount = Wordcount + ashape.TextFrame.TextRange.ComputeStatistics(Statistic:= wdStatisticWords) End If Next ashape MsgBox ("The document has " & Wordcount & " words") End Sub This method counts words approximately in a text string using RegExp, need reference to Microsoft Script Regular Expressions 5.5 I haven't tested for all possibilities but should be within shooting distance on count. There are too many exceptions. Heck in France Marie-Claire is one word; two words in English Sub testwordcount2() WordCount2 ("How many words.. are there in this user's dummy,,, multi line " & _ vbCrLf & "string.Count them.") WordCount2 (" the quick lazy dog ""dumb fink""!// ," & _ vbCrLf & vbLf & " eat's the cat") End Sub Function WordCount2(ByVal strSearch As String) As Long 'This replace all delimeters (punctuations, spaces,etc) with a 'space, trim the result string and and then count words. This will also take 'care of the string ending with puncutation or spaces Dim strPattern As String Dim ss As String Dim matches As VBScript_RegExp_55.MatchCollection Dim count As Long Dim myRegExp As New VBScript_RegExp_55.RegExp ss = "" ' pattern we want to use for replacement of junk characters ' by " " 'strPattern = "[\.:;,\s']" ' this doesn't take care of quotes strPattern = "[\.:;,\s""]" Set myRegExp = New RegExp myRegExp.Pattern = strPattern myRegExp.Global = True ss = myRegExp.Replace(strSearch, " ") Debug.Print ss ss = Trim(ss) strPattern = "\s+" myRegExp.Pattern = strPattern myRegExp.Global = True Set matches = myRegExp.Execute(ss) count = matches.count Debug.Print count + 1 & " words found<br>" WordCount2 = count End Function Susan Harkins wrote: >I'm looking for a macro that counts words on a single page and prints the >results. Anyone have something I could adapt? > >Susan H. > > -- Marty Connelly Victoria, B.C. Canada