Word count associated to a style

A

AussieBlueDog

Is it possible to use the word count function associated to a particular
style. If I format my document to have the body text set to a new Style
"BodyText" for example, and then via the NumWords field code, count only the
words typed in that style. This would allow me to keep track of word limits
in a uni assignment without counting the citations and headings,etc.

----------------
This post is a suggestion for Microsoft, and Microsoft responds to the
suggestions with the most votes. To vote for this suggestion, click the "I
Agree" button in the message pane. If you do not see the button, follow this
link to open the suggestion in the Microsoft Web-based Newsreader and then
click "I Agree" in the message pane.

http://www.microsoft.com/office/com...c970ee&dg=microsoft.public.word.docmanagement
 
S

Suzanne S. Barnhill

Not quite the same thing, and not automatic, but you can get a word count of
selected text (not with NumWords but through the UI).

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA
http://word.mvps.org
 
G

Greg Maxey

Not quite the same thing, and not automatic, but you can get a word countof
selected text (not with NumWords but through the UI).

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USAhttp://word.mvps.org








- Show quoted text -

No. But you might run a macro and assign the count to a document
variable. The result could then be displayed using a DocVariable
field:

Sub ComputeWordsPerStyle()
Dim oPar As Paragraph
Dim oCount As Long
Dim pStyleName As String
pStyleName = InputBox("Enter the style name to evaluate", "Sytle
Name")
oCount = 0
For Each oPar In ActiveDocument.Paragraphs
If oPar.Style = pStyleName Then
oCount = oCount + oPar.Range.ComputeStatistics(wdStatisticWords)
End If
Next oPar
With ActiveDocument
.Variables("WordCount").Value = oCount
.Fields.Update
End With
End Sub
 
A

AussieBlueDog

Thanx Suzanne, but that is a rather primitive and slow method. I do prefer
the "select all XX instances" method in the style menu and then using the
word count function under the Review tab. I was making a sugestion to
Microsoft to perhaps include an automatic method of doing this associated to
a field, perhaps called NumStyleWords, with a list of included styles to
count. As myself and most uni students are governed by a word count which
should not include citations, headings etc.
 
A

AussieBlueDog

Nice post Greg, Thank you.
The code seems to work, except for two minor issues so far.

1) The field in the document does not update when the macro runs, you still
have to right mouse click and update fields.

2) If different styles are used within the same paragraph it seems to not
count correctly. For example an inline citation would be in a different style
to the body text so as to not be counted.
 
G

Greg Maxey

You're welcome.

Problem 1 must be something on your end. It works here and the code:

With ActitiveDocument
.Fields.Update
End With

Is what should be updating your field.

The second issue is much more problematic (to my limited understanding). In
my defense, I would say that your current method appears to return the same
result ;-)

The ComputeStatistics method seems to do a reasonable job of sorting out
chaff that shouldn't be counted as words (e.g., punctuation. paragraph
marks, etc.). However, it does requires a range to evaluate and can't
filter out certain words if they happen to have a character style applied.
All I can think to do would be to evaluate each word object in each
paragraph formatted with the appropriate style. This complicates things
because a "word object" is different than a ComputeStatisitics word count.
A word object includes things like punctuation :-(

This might be close. It only counts word objects that starts with a letter
or number. For example this text results in a count of 16.
Heading 1: The periods and colon: are not counted. The {brackets} are not
counted. He ate 12 apples.



Option Explicit
Sub ComputeWordsPerStyle()
Dim oPar As Paragraph
Dim oCount As Long
Dim pStyleName As String
pStyleName = InputBox("Enter the style name to evaluate", "Sytle Name")
oCount = 0
For Each oPar In ActiveDocument.Paragraphs
If oPar.Style = pStyleName Then
oCount = oCount + WordCount(oPar)
End If
Next oPar
With ActiveDocument
.Variables("WordCount").Value = oCount
.Fields.Update
End With
End Sub
Function WordCount(ByRef oPara As Word.Paragraph)
Dim oRng As Word.Range
Dim oWord As Word.Range
Dim i As Long
i = 0
Set oRng = oPara.Range
For Each oWord In oRng.Words
If oWord.Style = "Normal" Then
Select Case True
Case Is = oWord.Characters.First Like "[A-Za-z0-9]"
i = i + oWord.ComputeStatistics(wdStatisticWords)
Case Else
' Skip
' Or
' If MsgBox("Do you want to count: " & oWord & "?", vbYesNo,
"Querry") = vbYes Then
' i = i + oWord.ComputeStatistics(wdStatisticWords)
' End If
End Select
End If
Next
WordCount = i
End Function
 
G

Greg Maxey

Problem 1 is probably caused by your field not being in the main text
storyrange. Try:

With ActiveDocument
.Variables("WordCount").Value = oCount
.PrintPreview
.ClosePrintPreview
End With
 
A

AussieBlueDog

Thanks again greg for a great post,

The code worked great except for a minor issue, that the style name was not
passed to the function, modified code is below.

Function WordCount(ByRef oPara As Word.Paragraph, ByVal pStyle As String)
Dim oRng As Word.Range
Dim oWord As Word.Range
Dim i As Long
i = 0
Set oRng = oPara.Range
For Each oWord In oRng.Words
If oWord.Style = pStyle Then
Select Case True
Case Is = oWord.Characters.First Like "[A-Za-z0-9]"
i = i + oWord.ComputeStatistics(wdStatisticWords)
Case Else
' Skip
' Or
' If MsgBox("Do you want to count: " & oWord & "?", vbYesNo,"Querry") =
vbYes Then
' i = i + oWord.ComputeStatistics(wdStatisticWords)
' End If
End Select
End If
Next
WordCount = i
End Function

The printpreview option did not work and caused the application to freeze
and then crash??

All this achieved, I do not mind having to manually update the field with a
right mouse click, I had to do that with the original NumWords anyway. There
must be an option that I have disabled, I will hunt it down..

Again, Thankyou very much.......................
 
G

Greg Maxey

ABD,

Yes, I was testing with "Normal" style and neglected to pass the style name
when I created the function. Glad you spotted the error was able to get it
to work. Puzzled by the field not updating. I have added a routine to
cycle through the storyranges to update fields. Give it a try:

Sub ComputeWordsPerStyle()
Dim oPar As Paragraph
Dim oCount As Long
Dim pStyleName As String
pStyleName = InputBox("Enter the style name to evaluate", "Sytle Name")
oCount = 0
For Each oPar In ActiveDocument.Paragraphs
If oPar.Style = pStyleName Then
oCount = oCount + WordCount(oPar, pStyleName)
End If
Next oPar
ActiveDocument.Variables("WordCount").Value = oCount
DocUpdateFields
End Sub
Function WordCount(ByRef oPara As Word.Paragraph, pStyle As String)
Dim oRng As Word.Range
Dim oWord As Word.Range
Dim i As Long
i = 0
Set oRng = oPara.Range
For Each oWord In oRng.Words
If oWord.Style = pStyle Then
Select Case True
Case Is = oWord.Characters.First Like "[A-Za-z0-9]"
i = i + 1
Case Else
' Skip
' Or
' If MsgBox("Do you want to count: " & oWord & "?", vbYesNo,
"Querry") = vbYes Then
' i = i + 1
' End If
End Select
End If
Next
WordCount = i
End Function
Sub DocUpdateFields()
Dim pRange As Word.Range
Dim iLink As Long
iLink = ActiveDocument.Sections(1).Headers(1).Range.StoryType
For Each pRange In ActiveDocument.StoryRanges
Do
pRange.Fields.Update
Set pRange = pRange.NextStoryRange
Loop Until pRange Is Nothing
Next
End Sub


--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org



AussieBlueDog said:
Thanks again greg for a great post,

The code worked great except for a minor issue, that the style name was
not
passed to the function, modified code is below.

Function WordCount(ByRef oPara As Word.Paragraph, ByVal pStyle As String)
Dim oRng As Word.Range
Dim oWord As Word.Range
Dim i As Long
i = 0
Set oRng = oPara.Range
For Each oWord In oRng.Words
If oWord.Style = pStyle Then
Select Case True
Case Is = oWord.Characters.First Like "[A-Za-z0-9]"
i = i + oWord.ComputeStatistics(wdStatisticWords)
Case Else
' Skip
' Or
' If MsgBox("Do you want to count: " & oWord & "?", vbYesNo,"Querry") =
vbYes Then
' i = i + oWord.ComputeStatistics(wdStatisticWords)
' End If
End Select
End If
Next
WordCount = i
End Function

The printpreview option did not work and caused the application to freeze
and then crash??

All this achieved, I do not mind having to manually update the field with
a
right mouse click, I had to do that with the original NumWords anyway.
There
must be an option that I have disabled, I will hunt it down..

Again, Thankyou very much.......................

Greg Maxey said:
Problem 1 is probably caused by your field not being in the main text
storyrange. Try:

With ActiveDocument
.Variables("WordCount").Value = oCount
.PrintPreview
.ClosePrintPreview
End With
 
G

Greg Maxey

ABD,

Be aware that the code may give unsatisfactory results with text involving
numbers. For example 1.1 is counted as two words. I have exhausted all my
ideas as it seems each time I fix one issue two more will pop up.

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org



AussieBlueDog said:
Thanks again greg for a great post,

The code worked great except for a minor issue, that the style name was
not
passed to the function, modified code is below.

Function WordCount(ByRef oPara As Word.Paragraph, ByVal pStyle As String)
Dim oRng As Word.Range
Dim oWord As Word.Range
Dim i As Long
i = 0
Set oRng = oPara.Range
For Each oWord In oRng.Words
If oWord.Style = pStyle Then
Select Case True
Case Is = oWord.Characters.First Like "[A-Za-z0-9]"
i = i + oWord.ComputeStatistics(wdStatisticWords)
Case Else
' Skip
' Or
' If MsgBox("Do you want to count: " & oWord & "?", vbYesNo,"Querry") =
vbYes Then
' i = i + oWord.ComputeStatistics(wdStatisticWords)
' End If
End Select
End If
Next
WordCount = i
End Function

The printpreview option did not work and caused the application to freeze
and then crash??

All this achieved, I do not mind having to manually update the field with
a
right mouse click, I had to do that with the original NumWords anyway.
There
must be an option that I have disabled, I will hunt it down..

Again, Thankyou very much.......................

Greg Maxey said:
Problem 1 is probably caused by your field not being in the main text
storyrange. Try:

With ActiveDocument
.Variables("WordCount").Value = oCount
.PrintPreview
.ClosePrintPreview
End With
 
A

AussieBlueDog

Thanx once again Greg for a great post,
The field update routine works...............

Now all I have to do is write all these research assignments. :)

Greg Maxey said:
ABD,

Be aware that the code may give unsatisfactory results with text involving
numbers. For example 1.1 is counted as two words. I have exhausted all my
ideas as it seems each time I fix one issue two more will pop up.

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org



AussieBlueDog said:
Thanks again greg for a great post,

The code worked great except for a minor issue, that the style name was
not
passed to the function, modified code is below.

Function WordCount(ByRef oPara As Word.Paragraph, ByVal pStyle As String)
Dim oRng As Word.Range
Dim oWord As Word.Range
Dim i As Long
i = 0
Set oRng = oPara.Range
For Each oWord In oRng.Words
If oWord.Style = pStyle Then
Select Case True
Case Is = oWord.Characters.First Like "[A-Za-z0-9]"
i = i + oWord.ComputeStatistics(wdStatisticWords)
Case Else
' Skip
' Or
' If MsgBox("Do you want to count: " & oWord & "?", vbYesNo,"Querry") =
vbYes Then
' i = i + oWord.ComputeStatistics(wdStatisticWords)
' End If
End Select
End If
Next
WordCount = i
End Function

The printpreview option did not work and caused the application to freeze
and then crash??

All this achieved, I do not mind having to manually update the field with
a
right mouse click, I had to do that with the original NumWords anyway.
There
must be an option that I have disabled, I will hunt it down..

Again, Thankyou very much.......................

Greg Maxey said:
Problem 1 is probably caused by your field not being in the main text
storyrange. Try:

With ActiveDocument
.Variables("WordCount").Value = oCount
.PrintPreview
.ClosePrintPreview
End With


On Mar 14, 11:53 pm, AussieBlueDog
Nice post Greg, Thank you.
The code seems to work, except for two minor issues so far.

1) The field in the document does not update when the macro runs, you
still
have to right mouse click and update fields.

2) If different styles are used within the same paragraph it seems to
not
count correctly. For example an inline citation would be in a different
style
to the body text so as to not be counted.



:
Not quite the same thing, and not automatic, but you can get a word
count of
selected text (not with NumWords but through the UI).

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USAhttp://word.mvps.org

message


Is it possible to use the word count function associated to a
particular
style. If I format my document to have the body text set to a new
Style
"BodyText" for example, and then via the NumWords field code,
count only
the
words typed in that style. This would allow me to keep track of
word
limits
in a uni assignment without counting the citations and
headings,etc..

----------------
This post is a suggestion for Microsoft, and Microsoft responds
to the
suggestions with the most votes. To vote for this suggestion,
click the "I
Agree" button in the message pane. If you do not see the button,
follow
this
link to open the suggestion in the Microsoft Web-based Newsreader
and then
click "I Agree" in the message pane.

http://www.microsoft.com/office/community/en-us/default.mspx?mid=7e97...Hide
quoted text -

- Show quoted text -

No. But you might run a macro and assign the count to a document
variable. The result could then be displayed using a DocVariable
field:

Sub ComputeWordsPerStyle()
Dim oPar As Paragraph
Dim oCount As Long
Dim pStyleName As String
pStyleName = InputBox("Enter the style name to evaluate", "Sytle
Name")
oCount = 0
For Each oPar In ActiveDocument.Paragraphs
If oPar.Style = pStyleName Then
oCount = oCount + oPar.Range.ComputeStatistics(wdStatisticWords)
End If
Next oPar
With ActiveDocument
.Variables("WordCount").Value = oCount
.Fields.Update
End With
End Sub- Hide quoted text -

- Show quoted text -
 
G

Greg Maxey

ABD,

Thanks.

I had a few minutes to kill this morning and tried a slightly different
approach to deal with numbers such as 1.1.1, phone numbers, and such. While
this is probably far from perfect, it seems to get pretty close for most
standard English text:

Sub ComputeWordsPerStyle()
Dim oPar As Paragraph
Dim oCount As Long
Dim pStyleName As String
pStyleName = InputBox("Enter the style name to evaluate", "Sytle Name")
oCount = 0
For Each oPar In ActiveDocument.Paragraphs
If oPar.Style = pStyleName Then
oCount = oCount + WordCount(oPar, pStyleName)
End If
Next oPar
ActiveDocument.Variables("WordCount").Value = oCount
DocUpdateFields
End Sub
Function WordCount(ByRef oPara As Word.Paragraph, pStyle As String)
Dim oRng As Word.Range
Dim oWord As Word.Range
Dim i As Long
i = 0
Set oRng = oPara.Range
For Each oWord In oRng.Words
If oWord.Style = pStyle Then
If oWord.Characters.First Like "[A-Za-z0-9]" Then
Select Case True
'Word following open grouping symbols, a space, or numerical
symbols
Case Is = InStr("""($#{[< ", oWord.Characters.First.Previous)
i = i + 1
Case Is = oWord.Characters.First.Previous = Chr(9) 'Word
following tab
i = i + 1
Case Is = oWord.Characters.First.Previous = Chr(11) 'Word
following line break
i = i + 1
Case Is = oWord Like oRng.Words(1) 'First word of paragraph
i = i + 1
Case Is = oWord.Characters.First.Previous = Chr(160) 'Word
following non-breaking space
i = i + 1
Case Is = oWord.Characters.First.Previous = Chr(147) 'Word
following open smart qoute
i = i + 1
Case Else
' Skip. Don't count
' Or
oWord.Select
If MsgBox("Do you want to count: " &
oWord.Characters.First.Previous + oWord & " as a separate word?", vbYesNo,
"Querry") = vbYes Then
i = i + 1
End If
End Select
Else
' Skip. Don't count
' Or
' If MsgBox("Do you want to count: " & oWord & "?", vbYesNo,
"Querry") = vbYes Then
' i = i + 1
' End If
End If
End If
Next
WordCount = i
End Function
Sub DocUpdateFields()
Dim pRange As Word.Range
Dim iLink As Long
iLink = ActiveDocument.Sections(1).Headers(1).Range.StoryType
For Each pRange In ActiveDocument.StoryRanges
Do
pRange.Fields.Update
Set pRange = pRange.NextStoryRange
Loop Until pRange Is Nothing
Next
End Sub

Thanx once again Greg for a great post,
The field update routine works...............

Now all I have to do is write all these research assignments. :)

Greg Maxey said:
ABD,

Be aware that the code may give unsatisfactory results with text
involving numbers. For example 1.1 is counted as two words. I have
exhausted all my ideas as it seems each time I fix one issue two
more will pop up.

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org



AussieBlueDog said:
Thanks again greg for a great post,

The code worked great except for a minor issue, that the style name
was not
passed to the function, modified code is below.

Function WordCount(ByRef oPara As Word.Paragraph, ByVal pStyle As
String) Dim oRng As Word.Range
Dim oWord As Word.Range
Dim i As Long
i = 0
Set oRng = oPara.Range
For Each oWord In oRng.Words
If oWord.Style = pStyle Then
Select Case True
Case Is = oWord.Characters.First Like "[A-Za-z0-9]"
i = i + oWord.ComputeStatistics(wdStatisticWords)
Case Else
' Skip
' Or
' If MsgBox("Do you want to count: " & oWord & "?",
vbYesNo,"Querry") = vbYes Then
' i = i + oWord.ComputeStatistics(wdStatisticWords)
' End If
End Select
End If
Next
WordCount = i
End Function

The printpreview option did not work and caused the application to
freeze and then crash??

All this achieved, I do not mind having to manually update the
field with a
right mouse click, I had to do that with the original NumWords
anyway. There
must be an option that I have disabled, I will hunt it down..

Again, Thankyou very much.......................

:

Problem 1 is probably caused by your field not being in the main
text storyrange. Try:

With ActiveDocument
.Variables("WordCount").Value = oCount
.PrintPreview
.ClosePrintPreview
End With


On Mar 14, 11:53 pm, AussieBlueDog
Nice post Greg, Thank you.
The code seems to work, except for two minor issues so far.

1) The field in the document does not update when the macro runs,
you still
have to right mouse click and update fields.

2) If different styles are used within the same paragraph it
seems to not
count correctly. For example an inline citation would be in a
different style
to the body text so as to not be counted.



:
On Mar 14, 9:55 am, "Suzanne S. Barnhill" <[email protected]>
wrote:
Not quite the same thing, and not automatic, but you can get a
word count of
selected text (not with NumWords but through the UI).

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USAhttp://word.mvps.org

in message


Is it possible to use the word count function associated to a
particular
style. If I format my document to have the body text set to a
new Style
"BodyText" for example, and then via the NumWords field code,
count only
the
words typed in that style. This would allow me to keep track of
word
limits
in a uni assignment without counting the citations and
headings,etc..

----------------
This post is a suggestion for Microsoft, and Microsoft responds
to the
suggestions with the most votes. To vote for this suggestion,
click the "I
Agree" button in the message pane. If you do not see the
button, follow
this
link to open the suggestion in the Microsoft Web-based
Newsreader and then
click "I Agree" in the message pane.

http://www.microsoft.com/office/community/en-us/default.mspx?mid=7e97...Hide
quoted text -

- Show quoted text -

No. But you might run a macro and assign the count to a document
variable. The result could then be displayed using a DocVariable
field:

Sub ComputeWordsPerStyle()
Dim oPar As Paragraph
Dim oCount As Long
Dim pStyleName As String
pStyleName = InputBox("Enter the style name to evaluate", "Sytle
Name")
oCount = 0
For Each oPar In ActiveDocument.Paragraphs
If oPar.Style = pStyleName Then
oCount = oCount +
oPar.Range.ComputeStatistics(wdStatisticWords) End If
Next oPar
With ActiveDocument
.Variables("WordCount").Value = oCount
.Fields.Update
End With
End Sub- Hide quoted text -

- Show quoted text -
 
J

Janine

Greg in your most recent post in 2007 I get an error at:

Case Is = InStr("""($#{[< ", oWord.Characters.First.Previous)
'Object variable or with block variable not set
Any idea why please?
Janine

Greg Maxey said:
ABD,

Be aware that the code may give unsatisfactory results with text involving
numbers. For example 1.1 is counted as two words. I have exhausted all
my ideas as it seems each time I fix one issue two more will pop up.

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org



AussieBlueDog said:
Thanks again greg for a great post,

The code worked great except for a minor issue, that the style name was
not
passed to the function, modified code is below.

Function WordCount(ByRef oPara As Word.Paragraph, ByVal pStyle As String)
Dim oRng As Word.Range
Dim oWord As Word.Range
Dim i As Long
i = 0
Set oRng = oPara.Range
For Each oWord In oRng.Words
If oWord.Style = pStyle Then
Select Case True
Case Is = oWord.Characters.First Like "[A-Za-z0-9]"
i = i + oWord.ComputeStatistics(wdStatisticWords)
Case Else
' Skip
' Or
' If MsgBox("Do you want to count: " & oWord & "?", vbYesNo,"Querry") =
vbYes Then
' i = i + oWord.ComputeStatistics(wdStatisticWords)
' End If
End Select
End If
Next
WordCount = i
End Function

The printpreview option did not work and caused the application to freeze
and then crash??

All this achieved, I do not mind having to manually update the field with
a
right mouse click, I had to do that with the original NumWords anyway.
There
must be an option that I have disabled, I will hunt it down..

Again, Thankyou very much.......................

Greg Maxey said:
Problem 1 is probably caused by your field not being in the main text
storyrange. Try:

With ActiveDocument
.Variables("WordCount").Value = oCount
.PrintPreview
.ClosePrintPreview
End With


On Mar 14, 11:53 pm, AussieBlueDog
Nice post Greg, Thank you.
The code seems to work, except for two minor issues so far.

1) The field in the document does not update when the macro runs, you
still
have to right mouse click and update fields.

2) If different styles are used within the same paragraph it seems to
not
count correctly. For example an inline citation would be in a
different style
to the body text so as to not be counted.



:
On Mar 14, 9:55 am, "Suzanne S. Barnhill" <[email protected]>
wrote:
Not quite the same thing, and not automatic, but you can get a
word count of
selected text (not with NumWords but through the UI).

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USAhttp://word.mvps.org

message


Is it possible to use the word count function associated to a
particular
style. If I format my document to have the body text set to a
new Style
"BodyText" for example, and then via the NumWords field code,
count only
the
words typed in that style. This would allow me to keep track of
word
limits
in a uni assignment without counting the citations and
headings,etc..

----------------
This post is a suggestion for Microsoft, and Microsoft responds
to the
suggestions with the most votes. To vote for this suggestion,
click the "I
Agree" button in the message pane. If you do not see the button,
follow
this
link to open the suggestion in the Microsoft Web-based
Newsreader and then
click "I Agree" in the message pane.

http://www.microsoft.com/office/community/en-us/default.mspx?mid=7e97...Hide
quoted text -

- Show quoted text -

No. But you might run a macro and assign the count to a document
variable. The result could then be displayed using a DocVariable
field:

Sub ComputeWordsPerStyle()
Dim oPar As Paragraph
Dim oCount As Long
Dim pStyleName As String
pStyleName = InputBox("Enter the style name to evaluate", "Sytle
Name")
oCount = 0
For Each oPar In ActiveDocument.Paragraphs
If oPar.Style = pStyleName Then
oCount = oCount + oPar.Range.ComputeStatistics(wdStatisticWords)
End If
Next oPar
With ActiveDocument
.Variables("WordCount").Value = oCount
.Fields.Update
End With
End Sub- Hide quoted text -

- Show quoted text -
 
G

Greg Maxey

Janine,

Oops. I think you would get the same error in any Word version if the Style
evaluated is applied to the first word in the document. Try adding the
following as the first Case:

Case Is = oWord = ActiveDocument.Words(1) 'Word is the first word in doc.
Greg in your most recent post in 2007 I get an error at:

Case Is = InStr("""($#{[< ",
oWord.Characters.First.Previous) 'Object variable or with block
variable not set Any idea why please?
Janine

Greg Maxey said:
ABD,

Be aware that the code may give unsatisfactory results with text
involving numbers. For example 1.1 is counted as two words. I have
exhausted all my ideas as it seems each time I fix one issue two
more will pop up. --
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org



AussieBlueDog said:
Thanks again greg for a great post,

The code worked great except for a minor issue, that the style name
was not
passed to the function, modified code is below.

Function WordCount(ByRef oPara As Word.Paragraph, ByVal pStyle As
String) Dim oRng As Word.Range
Dim oWord As Word.Range
Dim i As Long
i = 0
Set oRng = oPara.Range
For Each oWord In oRng.Words
If oWord.Style = pStyle Then
Select Case True
Case Is = oWord.Characters.First Like "[A-Za-z0-9]"
i = i + oWord.ComputeStatistics(wdStatisticWords)
Case Else
' Skip
' Or
' If MsgBox("Do you want to count: " & oWord & "?",
vbYesNo,"Querry") = vbYes Then
' i = i + oWord.ComputeStatistics(wdStatisticWords)
' End If
End Select
End If
Next
WordCount = i
End Function

The printpreview option did not work and caused the application to
freeze and then crash??

All this achieved, I do not mind having to manually update the
field with a
right mouse click, I had to do that with the original NumWords
anyway. There
must be an option that I have disabled, I will hunt it down..

Again, Thankyou very much.......................

:

Problem 1 is probably caused by your field not being in the main
text storyrange. Try:

With ActiveDocument
.Variables("WordCount").Value = oCount
.PrintPreview
.ClosePrintPreview
End With


On Mar 14, 11:53 pm, AussieBlueDog
Nice post Greg, Thank you.
The code seems to work, except for two minor issues so far.

1) The field in the document does not update when the macro runs,
you still
have to right mouse click and update fields.

2) If different styles are used within the same paragraph it
seems to not
count correctly. For example an inline citation would be in a
different style
to the body text so as to not be counted.



:
On Mar 14, 9:55 am, "Suzanne S. Barnhill" <[email protected]>
wrote:
Not quite the same thing, and not automatic, but you can get a
word count of
selected text (not with NumWords but through the UI).

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USAhttp://word.mvps.org

in message


Is it possible to use the word count function associated to a
particular
style. If I format my document to have the body text set to a
new Style
"BodyText" for example, and then via the NumWords field code,
count only
the
words typed in that style. This would allow me to keep track of
word
limits
in a uni assignment without counting the citations and
headings,etc..

----------------
This post is a suggestion for Microsoft, and Microsoft responds
to the
suggestions with the most votes. To vote for this suggestion,
click the "I
Agree" button in the message pane. If you do not see the
button, follow
this
link to open the suggestion in the Microsoft Web-based
Newsreader and then
click "I Agree" in the message pane.

http://www.microsoft.com/office/community/en-us/default.mspx?mid=7e97...Hide
quoted text -

- Show quoted text -

No. But you might run a macro and assign the count to a document
variable. The result could then be displayed using a DocVariable
field:

Sub ComputeWordsPerStyle()
Dim oPar As Paragraph
Dim oCount As Long
Dim pStyleName As String
pStyleName = InputBox("Enter the style name to evaluate", "Sytle
Name")
oCount = 0
For Each oPar In ActiveDocument.Paragraphs
If oPar.Style = pStyleName Then
oCount = oCount +
oPar.Range.ComputeStatistics(wdStatisticWords) End If
Next oPar
With ActiveDocument
.Variables("WordCount").Value = oCount
.Fields.Update
End With
End Sub- Hide quoted text -

- Show quoted text -
 
G

Greg Maxey

The reason for the error is that there is no previous character if the oWord
is the first word in the document.
Greg in your most recent post in 2007 I get an error at:

Case Is = InStr("""($#{[< ",
oWord.Characters.First.Previous) 'Object variable or with block
variable not set Any idea why please?
Janine

Greg Maxey said:
ABD,

Be aware that the code may give unsatisfactory results with text
involving numbers. For example 1.1 is counted as two words. I have
exhausted all my ideas as it seems each time I fix one issue two
more will pop up. --
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org



AussieBlueDog said:
Thanks again greg for a great post,

The code worked great except for a minor issue, that the style name
was not
passed to the function, modified code is below.

Function WordCount(ByRef oPara As Word.Paragraph, ByVal pStyle As
String) Dim oRng As Word.Range
Dim oWord As Word.Range
Dim i As Long
i = 0
Set oRng = oPara.Range
For Each oWord In oRng.Words
If oWord.Style = pStyle Then
Select Case True
Case Is = oWord.Characters.First Like "[A-Za-z0-9]"
i = i + oWord.ComputeStatistics(wdStatisticWords)
Case Else
' Skip
' Or
' If MsgBox("Do you want to count: " & oWord & "?",
vbYesNo,"Querry") = vbYes Then
' i = i + oWord.ComputeStatistics(wdStatisticWords)
' End If
End Select
End If
Next
WordCount = i
End Function

The printpreview option did not work and caused the application to
freeze and then crash??

All this achieved, I do not mind having to manually update the
field with a
right mouse click, I had to do that with the original NumWords
anyway. There
must be an option that I have disabled, I will hunt it down..

Again, Thankyou very much.......................

:

Problem 1 is probably caused by your field not being in the main
text storyrange. Try:

With ActiveDocument
.Variables("WordCount").Value = oCount
.PrintPreview
.ClosePrintPreview
End With


On Mar 14, 11:53 pm, AussieBlueDog
Nice post Greg, Thank you.
The code seems to work, except for two minor issues so far.

1) The field in the document does not update when the macro runs,
you still
have to right mouse click and update fields.

2) If different styles are used within the same paragraph it
seems to not
count correctly. For example an inline citation would be in a
different style
to the body text so as to not be counted.



:
On Mar 14, 9:55 am, "Suzanne S. Barnhill" <[email protected]>
wrote:
Not quite the same thing, and not automatic, but you can get a
word count of
selected text (not with NumWords but through the UI).

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USAhttp://word.mvps.org

in message


Is it possible to use the word count function associated to a
particular
style. If I format my document to have the body text set to a
new Style
"BodyText" for example, and then via the NumWords field code,
count only
the
words typed in that style. This would allow me to keep track of
word
limits
in a uni assignment without counting the citations and
headings,etc..

----------------
This post is a suggestion for Microsoft, and Microsoft responds
to the
suggestions with the most votes. To vote for this suggestion,
click the "I
Agree" button in the message pane. If you do not see the
button, follow
this
link to open the suggestion in the Microsoft Web-based
Newsreader and then
click "I Agree" in the message pane.

http://www.microsoft.com/office/community/en-us/default.mspx?mid=7e97...Hide
quoted text -

- Show quoted text -

No. But you might run a macro and assign the count to a document
variable. The result could then be displayed using a DocVariable
field:

Sub ComputeWordsPerStyle()
Dim oPar As Paragraph
Dim oCount As Long
Dim pStyleName As String
pStyleName = InputBox("Enter the style name to evaluate", "Sytle
Name")
oCount = 0
For Each oPar In ActiveDocument.Paragraphs
If oPar.Style = pStyleName Then
oCount = oCount +
oPar.Range.ComputeStatistics(wdStatisticWords) End If
Next oPar
With ActiveDocument
.Variables("WordCount").Value = oCount
.Fields.Update
End With
End Sub- Hide quoted text -

- Show quoted text -
 

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