Numbered List Count?

M

ML

Is there a way to get the total number of items in a numbered list?

I have a list with a unique style applied to it and I would like to get the
count of how many items are in the list so I can display the count at
another location in the document.

Any ideas?
 
S

Suzanne S. Barnhill

Insert a cross-reference to the paragraph number of the last item in the
list.
 
M

ML

I need something that will work without user intervention, ie this is a
custom template and I cannot depend on a user creating a cross reference. I
know that the style will be set but the number of references will always be
different so I cannot preset a cross-reference to this item. I need to
somehow do this in code after the fact if possible.
 
D

Daiya Mitchell

Then you should ask in a Word newsgroup that also has VBA or Programming in
the name.
 
M

ML

Sorry for asking I guess. I thought someone here might know since this is a
numbering group. The solution does not need to be VBA related, it just
needs to be automatic without the user making a reference.
 
D

Daiya Mitchell

Oh, I was thrown off by you saying you need to do it in code.

Anyhow, in general the way to do anything without user intervention is to
use a macro aka VBA. Even custom templates require user intervention. You
are asking for something complicated--you want to find the number of items
in a list that doesn't exist yet, and that involves several steps. I think
you will need VBA to find the list, count the items, and put the answer
somewhere. Word doesn't appear to have pre-defined fields for anything like
"# of paragraphs in a certain style" or "# of entries in a numbered list,"
which is what you would need.

It's be a nice feature if the NumParas field had such switches, but since
there doesn't even seem to be a NumParas field, guess not.
 
M

ML

"It's be a nice feature if the NumParas field had such switches, but since
there doesn't even seem to be a NumParas field, guess not."

Ok thanks, that is the kind of thing I was hoping for but I guess not :(
 
D

Daiya Mitchell

But I bet a NumParas field is really easy in VBA. Not so sure about
limiting it to certain styles, though I would find that feature really
useful as well.
 
G

G.G.Yagoda

Put a bookmarked named "ListCnt" where you want to display the list
count. Then run this code:

Dim R As Range, ListCnt As Integer
Set R = ActiveDocument.Range
R.Start = R.End
Do
With R.Find
.Style = "My Style"
.Forward = False
.Execute
Select Case R.Find.Found
Case True
If R.ListFormat.ListType = 3 Or R.ListFormat.ListType = 4
Then
ListCnt = R.ListFormat.ListValue
Exit Do
Else
ListCnt = ListCnt + 1
R.End = R.Start
End If
End Select
End With
Loop While R.Find.Found
Set R = ActiveDocument.Bookmarks("ListCnt").Range
R.Text = ListCnt
ActiveDocument.Bookmarks.Add Name:="ListCnt", Range:=R
 

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