Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can't count rows in microsoft word

Status
Not open for further replies.

must8657

Technical User
May 4, 2005
2
0
0
US
how do i count the number of rows in a selection (not table, but highlited area) for microsoft word. I can only seem to get it to work for tables.
thanks
 
Hi,

Try something like:

Sub GetLineCount()
Dim MyRange As Range, LineCount As Long
Set MyRange = Selection.Range
Selection.Collapse wdCollapseStart
While Selection.InRange(MyRange) = True
LineCount = LineCount + 1
Selection.MoveDown Unit:=wdLine, Count:=1
Wend
MyRange.Select
MsgBox LineCount
End Sub

Cheers
 
thanks, that works great. I wouldn't have thought it would have taken this much code.
jason
 
That is because "lines" (like "pages") has to be calculated every time you want to know what it is. Lines, like pages, are dynamic.

You make a selection, and the selected text is 120 points, with the paragraph spacing 40 points before, and after. The line count is, oh I don't know, say 10 - after all this is huge font/spacing.

Keeping the SAME selection, if the font size is changed to 8 point, paragraph spacing = 0, then the line count could be....say, 1. It is the same Selection - but a different line count.

Word has no way of knowing what the line count is until it actually goes out and looks at it. Only then can say what the current line count is. If you ask it again, it still has to go and calculate it, just like macropod's code does.

Gerry
See my Paintings and Sculpture
 
Hi Jason,

It doesn't need that much code!
Code:
[blue]MsgBox Selection.Range.ComputeStatistics(wdStatisticLines)[/blue]
... should work just as well.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Tony is right of course...I think I am having another stupid day, andi should probably get out of here.

.....still calculated though.

Gerry
See my Paintings and Sculpture
 
There is a significant difference in the line count returned by my macro compared to 'Selection.Range.ComputeStatistics(wdStatisticLines)'.

When you have a table included in the selection, 'Selection.Range.ComputeStatistics(wdStatisticLines)' will count every empty cell, for example as a line. My macro does not - it only counts each row. Things get even more complicated when cells on the same row in an included table have different line lengths. In that case, my macro counts the longest cell only, whereas 'Selection.Range.ComputeStatistics(wdStatisticLines)' adds together all cell lengths.

Arguably, then, my macro more closely matches the overall vertical number of lines that would be printed.

Cheers
PS: Word's line count function (under File|Page Setup|Layout deals with this bt ignoring tables altogether!
 
Good points, macropod. Word's handling of line counts is not obvious.

I disagree, however, about your code. It does not count the longest cell, it counts the first column (except when the selection is entirely within a single column. Also, to double-check this I cut and pasted your code, selected all and ran it and it didn't stop. If the selection includes the end of the document, the loop doesn't end.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Thanks for the feedback Tony,

I hadn't tried the code with the end of the document selected - obviously a bit more refinement is called for - and I'd only tested it with a table in which the second and subsequent columns had less rows than the first - yet more refinement needed (sigh).

Cheers
 
I suspect that all code always needs some refinement [lol]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top