Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Mailmerge and Fax / March 2007

Tip: Looking for answers? Try searching our database.

Inserting a field to count number of words in previous paragraph

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
andy.cotgreave@gmail.com - 21 Mar 2007 11:16 GMT
Hi,
I need to create a field which counts the number of words in the
preceeding paragraph.

And I'm actually completely stuck - VBA to navigate around a document
not something I have ever worked with. I'm pretty comfortable with
fields, but it's the paragraph selection that loses me.

Thanks in advance

Andy
Doug Robbins - Word MVP - 21 Mar 2007 14:04 GMT
It can't be done with fields alone.  You could use a DOCVARIABLE field and
then use VBA to load the number of words into the variable

To get the number of words in the ith paragraph of the document, you would
use

ActiveDocument.Paragraphs(i).Range.Words.Count

If you had a field { DOCVARIABLE varparai }

you could use

With ActiveDocument
   .Variables("varparai").Value = .Paragrapsh(i).Range.Words.Count
   .Range.Fields.Update
End With

to get the number of words to be displayed in that field.

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> Hi,
> I need to create a field which counts the number of words in the
[quoted text clipped - 7 lines]
>
> Andy
andy.cotgreave@gmail.com - 21 Mar 2007 15:10 GMT
On Mar 21, 1:04 pm, "Doug Robbins - Word MVP"
<d...@REMOVECAPSmvps.org> wrote:
> It can't be done with fields alone.  You could use a DOCVARIABLE field and
> then use VBA to load the number of words into the variable
>
> To get the number of words in the ith paragraph of the document, you would
> use

Hi Doug,
Thanks for the suggestion - sounds good.

One question remains, though! Each field needs to refer to the
paragraph above it - how do I work out the number of the paragraph in
the document relative to the position of the field?

Thanks

Andy
Doug Robbins - Word MVP - 22 Mar 2007 08:33 GMT
The following macro will add a field that displays [## words] at the end of
each paragraph in a document:

Dim i As Long
Dim varname As String
Dim frange As Range
With ActiveDocument
   For i = 1 To .Paragraphs.Count
       varname = "Para" & i
       .Variables(varname).Value = " [" & .Paragraphs(i).Range.Words.Count
& " words]"
       Set frange = .Paragraphs(i).Range
       frange.End = frange.End - 1
       frange.Collapse wdCollapseEnd
       .Fields.Add Range:=frange, Type:=wdFieldDocVariable, Text:=varname
   Next i
   .Fields.Update
End With

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> On Mar 21, 1:04 pm, "Doug Robbins - Word MVP"
> <d...@REMOVECAPSmvps.org> wrote:
[quoted text clipped - 16 lines]
>
> Andy
andy.cotgreave@gmail.com - 22 Mar 2007 10:29 GMT
On Mar 22, 7:33 am, "Doug Robbins - Word MVP"
<d...@REMOVECAPSmvps.org> wrote:
> The following macro will add a field that displays [## words] at the end of
> each paragraph in a document:
..snip...

> --
> Hope this helps.

Excellent!
Thanks for your help, that's just what I was after.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.