Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.mailmerge.fields
[email protected] andy.cotgreave@gmail.com is offline
external usenet poster
 
Posts: 9
Default Inserting a field to count number of words in previous paragraph

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

  #2   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Doug Robbins - Word MVP Doug Robbins - Word MVP is offline
external usenet poster
 
Posts: 8,832
Default Inserting a field to count number of words in previous paragraph

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.

--
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

wrote in message
oups.com...
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



  #3   Report Post  
Posted to microsoft.public.word.mailmerge.fields
[email protected] andy.cotgreave@gmail.com is offline
external usenet poster
 
Posts: 9
Default Inserting a field to count number of words in previous paragraph

On Mar 21, 1:04 pm, "Doug Robbins - Word MVP"
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

  #4   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Doug Robbins - Word MVP Doug Robbins - Word MVP is offline
external usenet poster
 
Posts: 8,832
Default Inserting a field to count number of words in previous paragraph

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


--
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

wrote in message
oups.com...
On Mar 21, 1:04 pm, "Doug Robbins - Word MVP"
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



  #5   Report Post  
Posted to microsoft.public.word.mailmerge.fields
[email protected] andy.cotgreave@gmail.com is offline
external usenet poster
 
Posts: 9
Default Inserting a field to count number of words in previous paragraph

On Mar 22, 7:33 am, "Doug Robbins - Word MVP"
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.

Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I set word count to not count words with three letters less? DianaHolmes Microsoft Word Help 8 September 24th 06 05:53 AM
count and show number of words in a line Ambrq Microsoft Word Help 0 November 23rd 05 04:25 AM
Sudden loss of words when inserting words.... Night Poet New Users 1 September 25th 05 09:54 PM
Formatting merge fields: inserting a paragraph mark before a field Justin Cascio Mailmerge 1 August 30th 05 08:18 PM
Inserting a field containing the number of pages in another sectio Mads Nielsen Microsoft Word Help 2 March 14th 05 09:09 PM


All times are GMT +1. The time now is 04:43 PM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"