Get Line Count of Each Table Cell

A

Alan Z. Scharf

Hi,

1. I have a table with two columns and 30 rows.

2. The first column has only a CustomerID, and the second column has
several
paragraphs of descriptive text.

3. I need to get the Line Count of each individual second column cell and
put that number in a new third column for each row.

4. Hence, I need to

A. Loop through each CustomerID,
B. Highlight the text in its second column cell,
C. Get the line count for that cell, i.e. same number that's available
in Tools, Wordcount...dialog box, and
D. Write that number into the adjacent third column cell for each
CustomerID.

Is this possible to do with VBA in Word?

I need the line count for each text cell so that when I import this table
into SQLServer I can set the vertical size of a Report text box according
to
how many lines of text there are.

Thanks very much.

Alan
 
D

Doug Robbins - Word MVP

You may be able to get the number of lines by getting the difference between
the Selection.Information (wdFirstCharacterLineNumber) for the first line in
the cell and for the last line in the cell. Either that or the difference
between the Selection.Information (wdVerticalPositionRelativeToPage) and
dividing that by the line spacing (usually about 120% of the font size for
single spacing)


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

Alan Z. Scharf

ks very much.

I will try that.

But first, I used an example from Greg Maxey in Tables group, and nearly
have what I need in code below.

Problem: I don't see an available property of LineCount comparable to the
Words.Count I used in my test below.

Question: Is there a Lines.Count property similar to the Words.Count
property?

It would seem there should be, since line count is available in Tools,
Wordcount... dialog.

Thanks.

Alan


Code to get line count of each cell(2) in each table row
-------------------------------------------------------
Public Sub CountLines()

'Purpose: To count lines in each cell's description

Dim tbl As Word.Tables
Dim cell As cell
Dim intCounter As Integer ' Counter for specifying which row's cell(2)
to write to


intCounter = 0
Set tbl = ActiveDocument.Tables
For Each cell In tbl(1).Columns(2).Cells
intCounter = (intCounter + 1)
-->> NEED a Lines.Count property here:
tbl(1).Columns(3).Cells(intCounter).Range.Text = cell.Range.Words.Count
Next

End Sub


..
 
J

Jay Freedman

Hi Alan,

You're exactly right that the Word Count dialog knows the line count. You
want something like this:

Dim dlg As Dialog
ActiveDocument.Tables(1).Cell(1, 1).Select
Selection.MoveEnd wdCharacter, -1
Set dlg = Dialogs(wdDialogToolsWordCount)
With dlg
.Execute
MsgBox "Lines: " & .Lines
End With

This appears, in limited testing, to work even when the row breaks across a
page boundary. If you were to try using the Selection.Information function,
you'd have to do a lot of extra work for that case.

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

Tony Jollans

Here is your/Greg's code amended to give the line count.

Public Sub CountLines()

'Purpose: To count lines in each cell's description

Dim tbl As Word.Tables
Dim cell As cell
Dim intCounter As Integer ' Counter for specifying which row's cell(2) to
write to


intCounter = 0
Set tbl = ActiveDocument.Tables
For Each cell In tbl(1).Columns(2).Cells
intCounter = (intCounter + 1)
'-->> NEED a Lines.Count property here:
tbl(1).Columns(3).Cells(intCounter).Range.Text = Range(cell.Range.Start,
cell.Range.End - 2).ComputeStatistics(wdStatisticLines)
Next

End Sub
 
A

Alan Z. Scharf

Jay,

Thanks very much. Worked perfectly and solved a big headache for me.

Tony in another message gave a different way to accomplish same thing.

Question: Is there really no Lines.Count property similar to
Paragraphs.Count and Words.Count?

That defies logic, since all three measures are given in Tools,
Wordcount...
dialog!

Regards,

Alan
 
A

Alan Z. Scharf

ks very much.

I had already used Jay's method by the time I received your message, but
your code opened my eyes to other commands I wasn't familiar with.

Question: How do you even discover these commands that you and Jay gave? Is
there a book you can recommend?

Regards,

Alan
 
J

Jay Freedman

Answer: There are often two, three, or more ways to accomplish the same
thing in Word (with or without VBA). Often the one you'd think is most
intuitively obvious doesn't work or has severe limitations.

No, there really is not a Lines.Count property, because there is no Lines
collection. The difference is that paragraphs, words, tables -- all the
things for which there are collections -- have independent existence as
objects in the document. Lines, in contrast, are created on the fly by the
pagination mechanism, which consults the current printer driver and
determines how much text fits between the current margins. (One consequence
of this is that line breaks may change if you select a different printer
driver -- see http://www.word.mvps.org/FAQs/Formatting/TextReflow.htm.)

Until Word 2003, there also was not a Pages collection, for the same reason.
I don't know how the MS developers managed to create such a collection,
because it seems to violate one of the basic tenets of the object model.

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

Jean-Guy Marcil

Jay Freedman was telling us:
Jay Freedman nous racontait que :
Answer: There are often two, three, or more ways to accomplish the
same thing in Word (with or without VBA). Often the one you'd think
is most intuitively obvious doesn't work or has severe limitations.

No, there really is not a Lines.Count property, because there is no
Lines collection. The difference is that paragraphs, words, tables --
all the things for which there are collections -- have independent
existence as objects in the document. Lines, in contrast, are created
on the fly by the pagination mechanism, which consults the current
printer driver and determines how much text fits between the current
margins. (One consequence of this is that line breaks may change if
you select a different printer driver -- see
http://www.word.mvps.org/FAQs/Formatting/TextReflow.htm.)
Until Word 2003, there also was not a Pages collection, for the same
reason. I don't know how the MS developers managed to create such a
collection, because it seems to violate one of the basic tenets of
the object model.
IN a way, the page object is still linked to the printer driver and screen
display becasue it is a property of the Pane object...:


--

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

Tony Jollans

The Page object is essentially graphical. It contains Rectangles which go to
making up the printed Page. Rectangles do have Lines Collections which may
contain Lines. AFAIK, however, they are of no use for this kind of purpose
because you have no control over the makeup of the Rectangles.
 
T

Tony Jollans

I can't speak for Jay but I discover them as a substitute for having a life
:) Slightly more seriously it comes from spending many many hours playing
with the toy, I mean application.

I own a single book on Word (not a programming book) - a gift from the
author - and have read no other so cannot in all honesty recommend one. I
have gained all my knowledge either first hand or from the web.
 
J

Jean-Guy Marcil

Tony Jollans was telling us:
Tony Jollans nous racontait que :
The Page object is essentially graphical. It contains Rectangles
which go to making up the printed Page. Rectangles do have Lines
Collections which may contain Lines. AFAIK, however, they are of no
use for this kind of purpose because you have no control over the
makeup of the Rectangles.

You can guess as to their content by using the type property... it gets
hairy though when you have tables, shapes, header, footers, etc. (One object
may have 2 or 3 associated rectagles.)

--

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

Jay Freedman

There are a few MVPs who "have a life", but they're rare. :) My wife
says that if I ever go missing, she'll be able to give the police a
good description of the back of my head.

I have a few books on Word, but none of them are more recent than Word
97, and I haven't looked at them in a long time. Mostly I use the VBA
help, the Object Browser, newsgroup posts, and Google.
 

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