Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / April 2005

Tip: Looking for answers? Try searching our database.

number of lines a cell

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jonas - 28 Apr 2005 21:44 GMT
What method would I use to calculate the number of lines in a cell within a
table programmatically?
Helmut Weber - 29 Apr 2005 09:16 GMT
Hi Jonas,
one method:

Sub LinesInCell()
Dim z1 As Integer
Dim z2 As Integer
With Selection
  .Cells(1).Select
  z1 = .Information(wdFirstCharacterLineNumber)
  .EndKey
  z2 = .Information(wdFirstCharacterLineNumber)
End With
  MsgBox z2 - z1 + 1
End Sub

But not reliable anymore, as in Word 2002
unlike in Word 97 all too often
.Information(wdFirstCharacterLineNumber) returns -1,
and as this does not provide for cells reaching over
page breaks.

Another method:

Sub LinesInCell_1()
Dim r As Long
Dim l As Long
With Selection
   .Cells(1).Select
   .Collapse
   l = 0
   r = .Cells(1).RowIndex
   On Error Resume Next
   Do Until r <> .Cells(1).RowIndex
     If Err.Number = 5941 Then
        Err.Clear
        Exit Do
     End If
     l = l + 1
     .MoveDown
   Loop
   .MoveUp ' or better restore original selection
End With
MsgBox l
End Sub

Counts lines even if the cell spans over several pages.
You might add some lines to get the selection back
to where it was before doing the counting.
And better rewrite it to a function.

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.