Word Count Macro

N

nrowson

I need to write a macro to count words according to my own needs:

Any set of characters (A-Z,a-z,0-9) with white space on either side,
including first and last word, excluding XHTML tags and hyphens.

I'm new to macros and would appreciate any help in doing this.

Thanks in advance.
 
H

Helmut Weber

Hi nrowson,

to get you started, see:
http://www.gmayor.com/replace_using_wildcards.htm

have you thought about words,
which are followed by a punctuation mark?

But you shall not stay without help.

I'd make a copy of the document in question
and try to remove all tags first, like that,
which won't give the desired result in all cases:

Sub Test5b()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "\<*\>"
.MatchWildcards = True
While .Execute
rDcm.Delete
Wend
End With
End Sub

Then you may make sure, the first and the last
character of each paragraph is a space, like that:

Sub Test5bx()
Dim oPrg As Paragraph
For Each oPrg In ActiveDocument.Paragraphs
If oPrg.Range.Characters.First <> " " Then
oPrg.Range.InsertBefore " "
End If
If oPrg.Range.Characters.Last.Previous <> " " Then
oPrg.Range.Characters.Last.InsertBefore " "
End If
Next
End Sub

Then the counting:

Sub Test5bxz()
Dim rDcm As Range
Dim lCnt As Long
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = " [a-zA-Z0-9.]{1,} "
.MatchWildcards = True
While .Execute
lCnt = lCnt + 1
Wend
End With
MsgBox lCnt
End Sub

Still some riddles, oddities and straightforward bugs
lurking behind.

--
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