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
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
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.
-------------------------------------------------------------------------------------------- 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[
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[
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).
I suspect that all code always needs some refinement
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[
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.