Switch for the NumWords field code to include footnotes?

K

KGB

I need the NumWords field code to display the total number of words,
including footnotes and endnotes. I can do this in the Word Count dialog by
selecting the "include textboxes, footnotes and endnotes" field. However, it
would save me a lot of time and hassle to be able to use the field code in
certain forms.
 
G

Greg Maxey

I need the NumWords field code to display the total number of words,
including footnotes and endnotes.  I can do this in the Word Count dialog by
selecting the "include textboxes, footnotes and endnotes" field.  However, it
would save me a lot of time and hassle to be able to use the field code in
certain forms.

AFAIK it can't be done. You might be able to calculate the Word count
programatically, save it to a variable and use a DocVariable field.

Something like this maybe:

Sub Test()
Dim i As Long
Dim pRange As Word.Range
Dim iLink As Long
iLink = ActiveDocument.Sections(1).Headers(1).Range.StoryType
For Each pRange In ActiveDocument.StoryRanges
Select Case pRange.StoryType
Case Is = 1, 2, 3
i = i + pRange.ComputeStatistics(wdStatisticWords)
Set pRange = pRange.NextStoryRange
End Select
Next
ActiveDocument.Variables("WordCount").Value = i - 1
ActiveDocument.Fields.Update
End Sub
 
M

macropod

As Greg says, there's no field code switch for this. Note too that headings, textboxes and hidden text are also excluded from the
count.
 
M

macropod

Actually, hidden text is counted/not counted according to whether Word's print option is set to print hidden text.

--
Cheers
macropod
[MVP - Microsoft Word]


macropod said:
As Greg says, there's no field code switch for this. Note too that headings, textboxes and hidden text are also excluded from the
count.

--
Cheers
macropod
[MVP - Microsoft Word]


KGB said:
I need the NumWords field code to display the total number of words,
including footnotes and endnotes. I can do this in the Word Count dialog by
selecting the "include textboxes, footnotes and endnotes" field. However, it
would save me a lot of time and hassle to be able to use the field code in
certain forms.
 
K

KGB

This was very helpful. Further research allowed me to shorten up the macro:

ActiveDocument.Variables("WordCount").Value =
ActiveDocument.ComputeStatistics(statistic:=wdStatisticWords,
IncludeFootnotesAndEndnotes:=True)

The two functions produce consistent results (off by one). The "range"
version you used should allow me (in the future) to develop a word count for
the entire document less those parts that I do not need to include. For now,
I'm just aiming for the gross word count.

I also set up a Macrobutton to perform the update:

MACROBUTTON Test "Update Word Count"

And, as you suggest, have inserted the variable into the document:

DOCVARIABLE WordCount

As far as I can tell, there is no way to have the macro run automatically
when the document prints, so that the variable updates like built-in
variables do.

The work-around I came up with is (1) use the macro to set a WordCount2
variable equal the normal word count (exclusing footnotes and endnotes) and
(2) to include an IF variable displaying text in the document to show whether
the variables are up to date.

IF {NumWords} = WordCount2 "Updated" "Not Updated"

The ideas is that the document would say: "This document includes 3,450
words (Updated)" or "This document includes 3,450 words (Not Updated)." This
would allow the user and recipients to visually confirm that the word count
if final.

However, this function always returns "Not Updated" even just after running
the macro. (For test purposes, I have both {NumWords} and {DOCVARIABLE
WordCount2} in the document, and I can visually confirm that they are equal.)

I then tried eliminating the spaces around the equal sign

IF {NumWords}=WordCount2 "Updated" "Not Updated"

This consistently returns "Updated" even if I add additional words without
running the macro, and I can visually see that the two variables are not equal

I think there's something basic I'm not getting. Any clues?
 
K

KGB

Thanks

macropod said:
Actually, hidden text is counted/not counted according to whether Word's print option is set to print hidden text.

--
Cheers
macropod
[MVP - Microsoft Word]


macropod said:
As Greg says, there's no field code switch for this. Note too that headings, textboxes and hidden text are also excluded from the
count.

--
Cheers
macropod
[MVP - Microsoft Word]


KGB said:
I need the NumWords field code to display the total number of words,
including footnotes and endnotes. I can do this in the Word Count dialog by
selecting the "include textboxes, footnotes and endnotes" field. However, it
would save me a lot of time and hassle to be able to use the field code in
certain forms.
 
K

KGB

Thanks to the help I received here and, in a separate thread, from Gordon
Bentley-Mix, I have now solved this issue better than I expected. Summary
and code below for those interested.

1) Bookmarks identifying the parts of the document that I do not need to count
2) A macro that counts up
(a) the entire document, including footnotes and endnotes),
(b) the parts I do not need to count, and
(c) notes the normal Word Count (excluding footnotes and endnotes) as of
the time the calculations were made.
3) Document fields codes that
(a) display the relevant information and
(b) include a macrobutton to run the macro. The Macrobutton even changes
labels from "Current" to "Update" depending on whether the displayed
information is current. (You do have to update the macrobutton manually, or
set it to update when printing, or you'll never see the "Update" warning.)

1) Bookmarks: ExcludedBeginning and ExcludedEnding
2) Macro:
Sub xxWordCount()
'
' xxWordCount Macro
' Note relationship b/t temp & doc variables, formally defined later
' ActiveDocument.Variables("xxWordCountAll").Value = a
' ActiveDocument.Variables("xxWordCountNoFN").Value = b
' ActiveDocument.Variables("xxWordCountExBeg").Value = c
' ActiveDocument.Variables("xxWordCountExEnd").Value = d
' ActiveDocument.Variables("xxWordCountExcl").Value = e
' ActiveDocument.Variables("xxWordCountable").Value = f

Dim a As Long
Dim b As Long
Dim c As Long
Dim d As Long
Dim e As Long
Dim f As Long

a = ActiveDocument.ComputeStatistics(Statistic:=wdStatisticWords,
IncludeFootnotesAndEndnotes:=True)
b = ActiveDocument.ComputeStatistics(Statistic:=wdStatisticWords,
IncludeFootnotesAndEndnotes:=False)

' Compute words used in excluded sections of brief
' Beginning sections
Dim ExBegRng As Word.Range
Set ExBegRng = ActiveDocument.Bookmarks("ExcludedBeginning").Range
c = ExBegRng.ComputeStatistics(Statistic:=wdStatisticWords)

' Ending sections
Dim ExEndRng As Word.Range
Set ExEndRng = ActiveDocument.Bookmarks("ExcludedEnding").Range
d = ExEndRng.ComputeStatistics(Statistic:=wdStatisticWords)

' All excluded sections

e = c + d

' Total Countable Words

f = a - e

ActiveDocument.Variables("xxWordCountAll").Value = a
ActiveDocument.Variables("xxWordCountNoFN").Value = b
ActiveDocument.Variables("xxWordCountExBeg").Value = c
ActiveDocument.Variables("xxWordCountExEnd").Value = d
ActiveDocument.Variables("xxWordCountExcl").Value = e
ActiveDocument.Variables("xxWordCountable").Value = f

ActiveDocument.Fields.Update
End Sub

3) Document text with field codes

This document contains {NUMWORDS \# "#,##0" } words per Microsoft Word’s
word count function and {DOCVARIABLE xxWordCountable \# "#,##0"} words ({
DOCVARIABLE xxWordCountAll \# "#,##0" } total - { DOCVARIABLE
xxWordCountExcl \# "#,##0" } excluded) { MACROBUTTON xxWordCount { IF
DocVariable xxWordCountNoFN } = {DOCPROPERTY Words "Current" "Update" } }
per our new word count macro.

The final product says:

This document contains 3,556 words per Microsoft Word’s word count function
and 3,703 words (4,037 total - 334 excluded) (Current) per our new word count
macro.

If I add one word (in a non-counted section), then print or otherwise update
without running the macro, it says:

This document contains 3,557 words per Microsoft Word’s word count function
and 3,703 words (4,037 total - 334 excluded) (Update) per our new word count
macro.
 
G

Greg

KGB,

I usually use OE to read newsgroups and have not seen either of your follow
ups there. Aware that there is a problem with posts replicating in a timely
manner I decided to poke in here.

What is "WordCount2" in your you IF field contructions? Is is a field of
some sort? You might try adding code to your project module that intercepts
the various print modes and force an update fields.
 

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