Displaying Table Row Count

F

Fred Mayfield

We have a table with approximately 100 rows. Below the table, we'd like to
have a text entry that shows the row count. Example:

Row 1
Row 2
Row 3

3 Entries

TYIA Fred.
 
P

Peter Hewett

Hi Fred

You can't do it without writing some code. If you've some Word macro
experience I'll explain what you need to do, it's not particularly
difficult:

Insert a bookmark in the table, preferably in the header row, somewhere
where it wont get deleted. If you don't have a header row in your table
just bookmark the ENTIRE table. Lets call the bookmark "TableToRowCount".

There's a number of different ways to achieve the next step but we'll use a
Docvariable field. Where you want the text "N Entries" insert a Docvariable
field: Insert>Field>Docvariable. In the Field dialog box after the text
DOCVARIABLE type "RowCount". Now add the text " Entries" after the field.

Now add the following Macro to the documents template:

Public Sub CountRows()
Const cBMName As String = "TableToRowCount"
Const cDVName As String = "RowCount"

Dim lngRows As Long
Dim strValue As String

' Find out the number of rows in the required table
With ActiveDocument.Bookmarks
If .Exists(cBMName) Then
lngRows = .Item(cBMName).Range.Tables(1).Rows.Count
Else
MsgBox "The current document does not contain the " & _
"expected bookmark: " & cBMName, vbExclamation
Exit Sub
End If
End With

' Find out if the Document Variable exists
On Error Resume Next
strValue = ActiveDocument.Variables(cDVName)
If Err.Number <> 0 Then

' It doesn't exist so create it
On Error GoTo 0
ActiveDocument.Variables.Add cDVName, CStr(lngRows)
Else

' Update existing variables
ActiveDocument.Variables(cDVName).Value = CStr(lngRows)
End If

' Update all fields in the BodyText part of the document
ActiveDocument.Fields.Update
End Sub


Now all you need is a way of triggering the macro so that it update the
DocumentVariable and the fields that displays the document variable value.

What I've done is create a custom macro to intercept the Word Save command:

Sub FileSave()
ActiveDocument.Save
CountRows
End Sub


So your displayed row count gets updated every time you save your document.

HTH + Cheers - Peter
 
J

Jean-Guy Marcil

Hi Peter,

Just in case you did not kow:
' Find out if the Document Variable exists
On Error Resume Next
strValue = ActiveDocument.Variables(cDVName)
If Err.Number <> 0 Then

' It doesn't exist so create it
On Error GoTo 0
ActiveDocument.Variables.Add cDVName, CStr(lngRows)
Else

' Update existing variables
ActiveDocument.Variables(cDVName).Value = CStr(lngRows)
End If

can be replaced by

ActiveDocument.Variables(cDVName) = CStr(lngRows)

If you did know, could please let me know why you go the long way. Is there
some reasons I should not use the shortcut?

Thanks

--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

Hi Jean-Guy and Peter,

To my mind, that's the advantage of using document variables rather than
custom document properties. It doesn't matter if the variable exists or
not. If you assign a value to it, it exists from that point on; if it was
already in existence, it's just been given a new value.

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.
Hope this helps
Doug Robbins - Word MVP
 
P

Peter Hewett

Hi JGM

The problems brain fade! and writing code at 3.00 am!

I do normally use the single line:
ActiveDocument.Variables("Name").Value = "Value"

It's why I use DVs for this kind of thing rather than
CustomDocumentProperties which require the sort of logic I incorrectly
wrote. Duuuuh!

Cheers - Peter
 
M

macropod

Hi Fred,

You could add another row to the bottom of your existing table with the
following field in any column:
{=COUNT(ABOVE)} Entries
where the braces are entered as a pair via Ctrl-F9. No vba required. Unlike
SUM(ABOVE), this works regardless of the contents of the cells in that
column. If you don't want to add a row to the table, bookmark it and use a
field like:
{=COUNT(BkMrk B:B)}
where BkMrk is the bookmark name and B:B is the colum you want to generate
the count from. In this case though, the column must have numbers in every
cell to get a true row count. Otherwise you'll only get a count of the rows
containing numbers (which may also be useful).

Cheers
 
F

Fred Mayfield

Excellent! Thanks all. Mother-in-law will be happy .... and more importantly
she'll be off my case!

FM
 

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