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 -