Count words in certain sections

B

BenCh

Hello!

I know how to count the words in my Microsoft Word document.
I also know how to create a field to display the document word count.

But I would really like to be create a field that automatically displays the
number of Words in a particular section or several particular sections of my
document.

I have an essay containing an abstract, glossary, title page and
bibliography, so I only want to count the words that I consider relevant. I'd
ideally like to display that total on the title page and have it update
automatically like my other fields do.

Does anybody know how this can be done please?

Many thanks in advance of your contributions.
 
J

Jay Freedman

Unfortunately, you're going to get -- at best -- part of what you want.
There is no field or set of field switches that can count the number of
words in a section, as opposed to the entire document.

You can get the count in a macro and place it into the title page, but it
isn't going to be automatically updated. In addition, if the number of
sections in the document changes for any reason (for example, to change the
number of columns or the page orientation for part of the text), you're
probably going to have to modify the macro.

The general idea is that the macro has a list of the numbers of the sections
that you consider "relevant" (since Word has no way of knowing which those
are). Let's say the title page is Section 1, the abstract is Section 2, the
body of the essay is Sections 3 through 6, and the glossary and bibligraphy
are sections 7 and 8. The "relevant" sections are 3, 4, 5, and 6 (maybe also
2 -- that's up to you, but you can see the possible confusion).

The macro would need statements like this -- one group for each relevant
section -- to collect the word count:

ActiveDocument.Sections(2).Range.Select
With Dialogs(wdDialogToolsWordCount)
.Execute
wdCount = wdCount + .Words
End With

(An actual production-quality macro would use an array of section numbers
and a For loop to call a function that runs the dialog, but this is the
basic idea.)

Once the macro has an accurate count of the words in the relevant sections,
it can store the number in a custom document property, and that number can
be displayed on the title page by a DocProperty field you insert there.

The macro would need a toolbar button or a keyboard shortcut to make it run
when you want to update the count -- there's no way to make it "run all the
time" the way Word's built-in word count can. You could also program a set
of macros that run when the document is saved or printed that would run the
word-count macro as part of those processes.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
B

BenCh

That's interesting, Jay.
Thank you, especially for the code snippet.

I'd be interested in producing such a macro. Unfortunately I'm working to a
tight deadline at the moment so cannot, but I shall consider it in the future.

Are macros still written in bog standard VB or have they been updated to VB
..NET yet?

Can request this feature in future versions of MS Word? I think a lot of
students would find it useful for their examined pieces of work.
 
B

BenCh

Also, if I were to embark on writing this macro, where could I find out
things like the objects and variables available to me?

How does MS Word define a section? Is it anything under a heading? Does it
have a concept of subsections or is it just one-dimensional?

Is there a way that I can define my own section in the page layout that
encompasses every subsection I consider to be relevant? I mean, perhaps, a
sort of bookmark saying "relevant material starts here" and "rel material
ends here".

Thanks again. Should I be asking these questions in the developers' forum?
 
G

grammatim

Every document starts life as 1 section. You can insert as many
Section Breaks as you wish -- and you will need to do so if you want
your front matter numbered with roman numerals (or unnumbered), or if
you want portions with different margin widths, number of columns,
etc.

How you do it depends on your version of Word.
 
J

Jay Freedman

Hi Ben,

For additional background on sections (and if you're asking questions like
these, you really need it), read
http://www.word.mvps.org/FAQs/Formatting/WorkWithSections.htm and
http://www.word.mvps.org/FAQs/Formatting/NumberingFrontMatter.htm.

In a macro, you refer to sections by their numbers. The first section is
ActiveDocument.Sections(1), the second is ActiveDocument.Sections(2), and so
forth. Although you could apply bookmarks, that's completely independent of
sections (and each bookmark in a document must have a unique name, so you
can't apply the same bookmark to two or more different places).

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
B

BenCh

I've done it.
Thanks for your help. It was an interesting introduction to macro development.
Ben
 

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